android 如何为片段中的按钮设置 onclick 侦听器

How to set onclick listener for a button in a fragment in android

我的应用包含如下图所示的表单:

当我点击菜单选项按钮时,抽屉打开如下图所示:

我希望抽屉在按下按钮 select 位置 时打开。

我的密码是

RegisterBlood.java

 ...
public class RegisterBlood extends Activity implements OnItemClickListener {
    DrawerLayout dLayout, dLayout2;
    ListView dList, dList2;
    ArrayAdapter<String> adapter, adapter2;
    String[] state_data2;
    String city = "", state = "";
    Button location;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.selectlocation);
        Bundle args = new Bundle();

        Fragment detail = new RegisterBloodFragment();
        detail.setArguments(args);
        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.content_frame, detail)
                .commit();

    ...
        dLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        dList = (ListView) findViewById(R.id.left_drawer);
        adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, state_data);
        dList.setAdapter(adapter);
        dList.setSelector(android.R.color.holo_blue_dark);
        // dLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
        dList.setOnItemClickListener(this);

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent e) {
        if (keyCode == KeyEvent.KEYCODE_MENU) {
            // your action...
            if (!dLayout.isDrawerOpen(dList)) {
                dLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
                dList = (ListView) findViewById(R.id.left_drawer);
                adapter = new ArrayAdapter<String>(this,
                        android.R.layout.simple_list_item_1, state_data2);
                dList.setAdapter(adapter);
                dLayout.openDrawer(dList);
                dList.setOnItemClickListener(this);

            }

            return true;
        }
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            // your action...
            if (dLayout.isDrawerOpen(dList)) {
                dLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
            }

            return true;
        }

        return super.onKeyDown(keyCode, e);
    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View v, final int position,
            long id) {
        // TODO Auto-generated method stub

        ...

        dLayout2 = (DrawerLayout) findViewById(R.id.drawer_layout);
        dList2 = (ListView) findViewById(R.id.left_drawer);
        adapter2 = new ArrayAdapter<String>(RegisterBlood.this,
                android.R.layout.simple_list_item_1, city_data);
        dList2.setAdapter(adapter2);
        dList2.setSelector(android.R.color.holo_blue_dark);
        dLayout2.openDrawer(dList2);
        dLayout2.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_OPEN);
        dList2.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View v, int position2,
                    long id) {
                dLayout2.closeDrawers();
                state = state_data2[position];
                city = city_data[position2];

                Bundle args = new Bundle();
                args.putString("Menu", city_data[position2] + " ("
                        + state_data2[position] + ")");
                Fragment detail = new RegisterBloodFragment();
                detail.setArguments(args);
                FragmentManager fragmentManager = getFragmentManager();
                fragmentManager.beginTransaction()
                        .replace(R.id.content_frame, detail).commit();

            }
        });

    }}

RegisterBloodFragment.java

...

public class RegisterBloodFragment extends Fragment implements OnClickListener {
    Button location;
    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle args) {
        View view = inflater.inflate(R.layout.registerblood, container, false);
        String menu = getArguments().getString("Menu");
        location= (Button) view.findViewById(R.id.etlocation);
        location.setText(menu);
        //Context c=getActivity();
        //location.setOnClickListener(c.getApplicationContext().set);
        return view;
    }
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Toast.makeText(getActivity(),
                "Yes",
                Toast.LENGTH_SHORT).show();

    }

}

registerblood.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/silver"
    android:gravity="center"
    android:orientation="vertical"
    android:paddingBottom="10dp"
    android:paddingLeft="40dp"
    android:paddingRight="40dp"
    android:paddingTop="10dp" >

    <EditText
        android:id="@+id/bregetName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="18dp"
        android:ems="10"
        android:hint="Name" />

    <EditText
        android:id="@+id/bregetBloodGroup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="18dp"
        android:ems="10"
        android:hint="Blood Group" />

        <Button
            android:id="@+id/etlocation"
            style="@android:style/Widget.EditText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="14dp"
            android:ems="10"
            android:hint="select location" >
        </Button>

    <Button
        android:id="@+id/bregbtnSignUp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginBottom="10sp"
        android:layout_marginTop="10sp"
        android:background="@drawable/button"
        android:shadowColor="#A8A8A8"
        android:shadowDx="0"
        android:shadowDy="0"
        android:shadowRadius="5"
        android:text="Submit"
        android:textColor="#FFFFFF"
        android:textSize="24sp" />

</LinearLayout>

selectlocation.xml

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
     >


</FrameLayout>

 <ListView android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="end"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    android:background="#fff"/>

我的问题是:

如何为RegisterBlood.java中的select位置按钮添加onclicklistener或从RegisterBloodFragment.java调用RegisterBlood.java中的onClickListener

请帮帮我。

由于按钮是片段布局的一部分,因此在此处设置侦听器:

@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle args) {
    View view = inflater.inflate(R.layout.registerblood, container, false);
    String menu = getArguments().getString("Menu");
    location = (Button) view.findViewById(R.id.etlocation);
    location.setText(menu);
    location.setOnClickListener(this);

    return view;
}

@Override
public void onClick(View v) {
    RegisterBlood activity = (RegisterBlood) getActivity();
    // Now you can contact your activity through activity e.g.:
    activity.onKeyDown(KeyEvent.KEYCODE_MENU, null);
}

首先记得实现OnClickListener接口:

public class YourClassName extends Fragment implements View.OnClickListener

public Button button;

然后,在方法 OnCreateView 中,为按钮充气并为其设置侦听器,如下所示:

@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle args) {

    button = (Button) inflater.inflate(R.layout.registerblood, container, false).findViewById(R.id.your_button_id);
    button.setOnClickListener(this);

}

然后 @Override 函数 onClick 然后做任何你想做的事:

@Override
public void onClick(View v) {
    //YOUR CODE HERE
}

EDIT 如果您只是在寻找 FocusListener(在这种情况下您的问题需要更新),请在您的 onCreateView 方法上设置侦听器:

your_edittext.setOnFocusChangeListener(new OnFocusChangeListener() {

@Override
public void onFocusChange(View v, boolean hasFocus) {
    if (hasFocus) {
        Toast.makeText(getApplicationContext(), "got the focus", Toast.LENGTH_LONG).show();
        // OPEN THE DRAWER HERE
    } else {
         Toast.makeText(getApplicationContext(), "lost the focus", Toast.LENGTH_LONG).show();
    }
}

或者您可以为您的 class 实施该侦听器。

你需要像这样为按钮点击膨胀片段

  View view = inflater.inflate(R.layout.fragment_blank3,
            container, false);
   Button bt1=(Button)view.findViewbyId(R.id.buttton);
   bt1.setOnclick...
   //then return in on create view.
    return view;

// 这个很好用,谢谢