web-dev-qa-db-fra.com

Comment définir onclick listener pour un bouton dans un fragment sous Android

Mon application contient un formulaire comme indiqué dans l'image suivante:

register screenshot

Lorsque je clique sur le bouton des options du menu, le tiroir s'ouvre comme indiqué dans l'image suivante: select screenshot

Je souhaite que le tiroir s'ouvre lorsque vous appuyez sur le bouton sélectionner l'emplacement .

Mes codes sont

S'inscrireBlood.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();

            }
        });

    }}

Inscrivez-vousBloodFragment.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"/>

Ma question est:

Comment ajouter onclicklistener pour le bouton de sélection d'emplacement dans RegisterBlood.Java ou appeler onClickListener qui est dans RegisterBlood.Java à partir de RegisterBloodFragment.Java

Sil te plait aide moi.

6
Abhishek E H

Le bouton faisant partie de la présentation du fragment, définissez le programme d'écoute à cet endroit:

@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);
}
15
Simas

Tout d'abord, n'oubliez pas d'implémenter l'interface OnClickListener:

public class YourClassName extends Fragment implements View.OnClickListener

public Button button;

Ensuite, dans la méthode OnCreateView, gonflez le bouton et configurez-le comme suit:

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

}

Puis @Override la fonction onClick et faites ce que vous devez faire:

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

EDIT Si vous cherchez simplement une FocusListener (dans ce cas, votre question nécessite une mise à jour), définissez le programme d'écoute sur votre méthode 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();
    }
}

ou vous pouvez implémenter cet auditeur dans votre classe.

9
Stefano Munarini

vous devez gonfler un fragment comme celui-ci pour cliquer sur un bouton

  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;
0
Syed Danish Haider