OnClickListener inside custom alertdialog Android

2019-04-22 04:00发布

问题:

I am a self-taught beginner and appreciate patience! Thanks!

In Eclipse, I made a custom alertdialog with its own xml file ("custom_dialog") and it is called "usernamealert".

I want an alert to pop-up if the user hasn't entered a username yet (ie, username.length == 0).

Inside this layout I have a textView ("What is your name?"), editText and button ("usernameButton").

Before putting in the onclicklistener for the button, everything worked. This was my (relevant) Java:

LayoutInflater inflater = getLayoutInflater();
View dialoglayout = inflater.inflate(R.layout.custom_dialog, (ViewGroup)     getCurrentFocus());
AlertDialog.Builder usernamebuilder = new AlertDialog.Builder(this);
usernamebuilder.setView(dialoglayout);

AlertDialog usernamealert = usernamebuilder.create();

When I put the onclicklistener in, it broke! Where should I have put it?

(the following is what I had tried... all in my OnCreate)

LayoutInflater inflater = getLayoutInflater();
View dialoglayout = inflater.inflate(R.layout.custom_dialog, (ViewGroup)getCurrentFocus());
AlertDialog.Builder usernamebuilder = new AlertDialog.Builder(this);
usernamebuilder.setView(dialoglayout);


Button usernameButton = (Button) usernamealert.findViewById(R.id.usernameButton);
usernameButton.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {  
//store username in sharedprefs
usernamealert.dismiss();



}
});

After the code I said:

if (username.length() == 0) {
            usernamealert.show();
        }

Again, it worked before I started messing with the button!!

回答1:

It is needed to specify where will the code search the button, if its only "findViewById" it would search in the xml of the host, it should be

LayoutInflater inflater =getLayoutInflater();
                View myview = inflater.inflate(R.layout.dialoghireteachernegotiate, null);
                // Inflate and set the layout for the dialog
                // Pass null as the parent view because its going in the dialog layout
                builder.setView(myview);
                 Button addS = (Button) myview.findViewById (R.id.bAddS);

This is part of my class, Hireteachernegotiate.class, which has a layout of hireteachernegotiate.xml

 AlertDialog.Builder builder = new AlertDialog.Builder(this);
                // Get the layout inflater
                LayoutInflater inflater =getLayoutInflater();
                View myview = inflater.inflate(R.layout.dialoghireteachernegotiate, null);
                // Inflate and set the layout for the dialog
                // Pass null as the parent view because its going in the dialog layout
                builder.setView(myview);

                Button addS = (Button) myview.findViewById (R.id.bAddS);
                addS.setOnClickListener(new View.OnClickListener() {

                    public void onClick(View v) {
                        //do some stuff
                    }
                });

                Button minusS = (Button) myview.findViewById (R.id.bMinusS);
                addS.setOnClickListener(new View.OnClickListener() {

                    public void onClick(View v) {
                       //do other stuff
                    }
                });

                // Add action buttons
                       builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
                           public void onClick(DialogInterface dialog, int id) {


                               dialog.cancel();
                           }
                       });
                   builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int id) {
                           dialog.cancel();
                       }
                   });   



            builder.show();

This is the dialog layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

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


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

</LinearLayout>


回答2:

Try this.

usernamebuilder.setCancelable(false)
usernamebuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int id) {
            //do what you want.
       }
   });

See if that works, or if it helps in some way.