简体   繁体   中英

Android - Click Button Inside alertDialog

I wonder how to click a Button inside of AlertDialog in Android and this is my code

activity_float_info.xml

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

MainActivity.java

 QR_ = LayoutInflater.from(MainActivity.this).inflate(R.layout.activity_float_info, null);
        MAIN_QR_SCAN = (LinearLayout) findViewById(R.id.MAIN_QR_SCAN);

        MAIN_QR_SCAN.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new AlertDialog.Builder(MainActivity.this)
                        .setCancelable(Boolean.TRUE)
                        .setNegativeButton(getString(R.string.CANCEL), new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                dialogInterface.dismiss();
                            }
                        })
                        .setView(R.layout.activity_float_info)
                        .show();

                button = (Button)QR_.findViewById(R.id.button);
                button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        button.setText("TEst");
                    }
                });
            }
        });

I have inflate the layout.. I think the main problem is on

button = (Button)QR_.findViewById(R.id.button);

try this create a layout for dialog box

<TextView android:id="@+id/dialogtitle"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:textColor="@android:color/black"
    android:text="Please enter the email address you used for the account"
    />
<EditText
    android:id="@+id/emailedittext"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:ems="10"
    android:padding="5dp"
    android:cursorVisible="true"
    android:singleLine="true"
    android:background="@android:color/white"
    android:textColor="@android:color/black"
    android:hint="Enter Mail id"
    android:textSize="20dp" >
    <requestFocus />
</EditText>

<LinearLayout android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:weightSum="2"
    android:orientation="horizontal">

     <Button 
        android:id="@+id/cancelbtn"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:text="CANCEL"/>

      <Button 
        android:id="@+id/okbtn"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:text="Ok"/>
</LinearLayout>

Then create a dialog box using this layout and handle button clicks

final Dialog dialog = new Dialog(MainActivity.this);
      // Include dialog.xml file
      dialog.setContentView(R.layout.forgotpassword);
      // Set dialog title
      dialog.setTitle("ALERT!!");
      // set values for custom dialog components - text, image and button
      Button okbtn = (Button) dialog.findViewById(R.id.okbtn);
      Button cancelbtn = (Button) dialog.findViewById(R.id.cancelbtn);
      final EditText emailedittext = (EditText) dialog.findViewById(R.id.emailedittext);
      dialog.show();
      dialog.getWindow().setSoftInputMode(
              WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
      // if decline button is clicked, close the custom dialog
      cancelbtn.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              // Close dialog
              dialog.dismiss();
          }
      });
      okbtn.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              String email=emailedittext.getText().toString();
              //do something more here
          }
      });

Refer: https://coderzpassion.com/android-show-alertdialog/

Here is a hack to use AlertDialog :

public class CustomDialog extends AlertDialog {

    protected CustomDialog(Context context) {
        super(context);
    }
}

And then in your Activity:

        CustomDialog dialog = new CustomDialog(this);
        View view = getLayoutInflater().inflate(R.layout.custom_dialog_layout,null);
        dialog.setView(view);
        Button button = (Button)view.findViewById(R.id.custom_button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(YourActivity.this,"Your message", Toast.LENGTH_LONG).show();
            }
        });
        dialog.show();

And the layout of the dialog (which has nothing in it barring the Button):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/custom_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="New Button" />

</RelativeLayout>

And you should be able to see a Toast when you click on the button. Hope this helps.

Dialog is like a popup window to show some options to users(options like accept/decline).

Using class android.app.Dialog to create dialog.

Using dialog.xml file to create custom dialog layout.

Example: res/layout/dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<ImageView
    android:id="@+id/imageDialog"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="6dp" />

<TextView
    android:id="@+id/textDialog"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="#FFF"
    android:layout_toRightOf="@+id/imageDialog"/>

 <Button
    android:id="@+id/declineButton"
    android:layout_width="100px"
    android:layout_height="wrap_content"
    android:text=" Submit "
    android:layout_marginTop="5dp"
    android:layout_marginRight="5dp"
    android:layout_below="@+id/textDialog"
    android:layout_toRightOf="@+id/imageDialog"
    />

</RelativeLayout>

Java Code

// Create custom dialog object
final Dialog dialog = new Dialog(CustomDialog.this);
// Include dialog.xml file
dialog.setContentView(R.layout.dialog);
// Set dialog title
dialog.setTitle("Custom Dialog");

// set values for custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.textDialog);
text.setText("Custom dialog Android example.");
ImageView image = (ImageView) dialog.findViewById(R.id.imageDialog);
image.setImageResource(R.drawable.image0);

dialog.show();

Button declineButton = (Button) dialog.findViewById(R.id.declineButton);
// if decline button is clicked, close the custom dialog
declineButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Close dialog
dialog.dismiss();
}
});

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