简体   繁体   中英

How to use Button click function on Alert Dialog in android?

I am building an Android application where on click of a button an Alert dialog opens which has a spinner and a button.

How can I implement onClick function on button that shown in Alert Dialog Box?

Here is my code of Alert Dialog:

private void viewCategory() {

    AlertDialog.Builder viewDialog = new AlertDialog.Builder(getActivity());

    viewDialog.setTitle("Host Your Game");

    LayoutInflater li = (LayoutInflater)   
    getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View dialogView = li.inflate(R.layout.customealertdialogbox, null);
    viewDialog.setView(dialogView);

    viewDialog.setPositiveButton("Ok",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                }
            });

    viewDialog.setNegativeButton("Cancel",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                }
            });
    viewDialog.show();



    Spinner spinnercategory = (Spinner) dialogView
            .findViewById(R.id.spinner1);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
            getActivity(), R.array.SportSelection, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinnercategory.setAdapter(adapter);

    Spinner spinnercategory2 = (Spinner) dialogView
            .findViewById(R.id.spinner2);
    ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(
            getActivity(), R.array.time, android.R.layout.simple_spinner_item);
    adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinnercategory2.setAdapter(adapter2);

    Spinner spinnercategory3 = (Spinner) dialogView
            .findViewById(R.id.spinner3);

    ArrayAdapter<CharSequence> adapter3 = ArrayAdapter.createFromResource(
            getActivity(), R.array.Noofplayer, android.R.layout.simple_spinner_item);
    adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinnercategory3.setAdapter(adapter3);

    Spinner spinnercategory4 = (Spinner) dialogView
            .findViewById(R.id.spinner4);       
    ArrayAdapter<CharSequence> adapter4 = ArrayAdapter.createFromResource(
            getActivity(), R.array.requirement, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinnercategory4.setAdapter(adapter4);

    spinnercategory.setOnItemSelectedListener(new OnItemSelectedListener() {

        public void onItemSelected(AdapterView<?> parent, View arg1,
                int arg2, long arg3) {
            String selItem = parent.getSelectedItem().toString();
            Toast.makeText(getActivity(), selItem,
                       Toast.LENGTH_LONG).show();
        }

        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub
        }
    });

Here is Xml code of this layout -

<?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:orientation="vertical"
   >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Select Your Sport :"
    android:layout_marginLeft="11sp"
    android:layout_marginTop="5sp"
    android:textSize="20sp"
    />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button" />

  <Spinner
     android:id="@+id/spinner1"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_gravity="center"
      />

  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Sport Date :"
    android:layout_marginLeft="11sp"

    android:textSize="20sp"
    />

  <DatePicker
      android:id="@+id/datePicker1"
      android:calendarViewShown="false"
      android:layout_width="match_parent"
      android:layout_height="150sp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Select Your Time_Slot :"
    android:layout_marginLeft="11sp"

    android:textSize="20sp"
    />

     <Spinner
         android:id="@+id/spinner2"
         android:layout_width="match_parent"
         android:layout_height="wrap_content" />

      <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Max. No. Player in Sport :"
    android:layout_marginLeft="11sp"     
    android:textSize="20sp"
    />

     <Spinner
         android:id="@+id/spinner3"
         android:layout_width="match_parent"
         android:layout_height="wrap_content" />

      <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Min. No. Player in Sport :"
    android:layout_marginLeft="11sp"       
    android:textSize="20sp"
    />

      <Spinner
          android:id="@+id/spinner4"
          android:layout_width="match_parent"
          android:layout_height="wrap_content" />

  </LinearLayout>

after these line...

View dialogView = li.inflate(R.layout.customealertdialogbox, null);
viewDialog.setView(dialogView);

add....

final Button button1=(Button)dialogView.findViewById(R.id.button1);//button1 is your button-id

and then use this object ...and do whatever you like to

            button1.setOnClickListener(new OnClickListener() 
            {
                @Override
                public void onClick(View v) 
                {
                    //your logic
                }
            });

Frist you have to initialize button and set click listener before shown alert dialog :

final Button button1 = (Button) dialogView.findViewById(R.id.button1);

button1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
       Toast.makeText(context, "button1 click",Toast.LENGTH_LONG).show();
    }
});

Formmated code :

   private void viewCategory(final Context context) {

        AlertDialog.Builder viewDialog = new AlertDialog.Builder(context);
        viewDialog.setTitle("Host Your Game");

        View dialogView = LayoutInflater.from(context).inflate(R.layout.customealertdialogbox, null);
        viewDialog.setView(dialogView);
        final Spinner spinnercategory = (Spinner) dialogView.findViewById(R.id.spinner1);
        final Spinner spinnercategory2 = (Spinner) dialogView.findViewById(R.id.spinner2);
        final Spinner spinnercategory3 = (Spinner) dialogView.findViewById(R.id.spinner3);
        final Spinner spinnercategory4 = (Spinner) dialogView.findViewById(R.id.spinner4);
        final Button button1 = (Button) dialogView.findViewById(R.id.button1);

        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(context, R.array.SportSelection, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnercategory.setAdapter(adapter);


        ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(context, R.array.time, android.R.layout.simple_spinner_item);
        adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnercategory2.setAdapter(adapter2);


        ArrayAdapter<CharSequence> adapter3 = ArrayAdapter.createFromResource(context, R.array.Noofplayer, android.R.layout.simple_spinner_item);
        adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnercategory3.setAdapter(adapter3);


        ArrayAdapter<CharSequence> adapter4 = ArrayAdapter.createFromResource(context, R.array.requirement, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnercategory4.setAdapter(adapter4);

        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context, "button1 click",Toast.LENGTH_LONG).show();
            }
        });

        spinnercategory.setOnItemSelectedListener(new OnItemSelectedListener() {

            public void onItemSelected(AdapterView<?> parent, View arg1,int arg2, long arg3) {
                Toast.makeText(context, spinnercategory.getSelectedItem().toString(),Toast.LENGTH_LONG).show();
            }

            public void onNothingSelected(AdapterView<?> arg0) {
            }
        });
        viewDialog.show();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM