Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在android中为片段中的按钮设置onclick侦听器_Android_Android Fragments_Onclick_Drawer - Fatal编程技术网

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

如何在android中为片段中的按钮设置onclick侦听器,android,android-fragments,onclick,drawer,Android,Android Fragments,Onclick,Drawer,我的应用程序包含一个表单,如下图所示: 单击菜单选项按钮时,抽屉将打开,如下图所示: 我想在按下按钮选择位置时打开抽屉 我的密码是 RegisterBlood.java ... public class RegisterBlood extends Activity implements OnItemClickListener { DrawerLayout dLayout, dLayout2; ListView dList, dList2; ArrayAdapter&

我的应用程序包含一个表单,如下图所示:

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

我想在按下按钮选择位置时打开抽屉

我的密码是

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();

            }
        });

    }}
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
中的选择位置按钮添加
onclicklistener
,或从
RegisterBloodFragment.java
调用
onclicklistener
中的
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);

}
然后,
@覆盖
函数onClick并执行您必须执行的操作:

@Override
public void onClick(View v) {
    //YOUR CODE HERE
}
编辑
如果您只是在寻找
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();
    }
}

或者您可以将该侦听器实现到您的类中。

您需要像这样膨胀片段,以便单击按钮

  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;

//这很好,谢谢

但我想打开那个抽屉(从BloodRegistration.java的onKeyDown调用)而不是toast。我把吐司放在那里只是为了测试它是否有效。如何从bloodRegistrationFragment.java创建抽屉?非常感谢。。。我刚得到我想要的:)。我用一个方法替换了onKeyDown,这样它只有在单击该按钮时才能工作。
  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;