自定义alertdialog Android中的OnClickListener

时间:2012-08-28 22:22:28

标签: android button alertdialog

我是一名自学成才的初学者,并且非常耐心!谢谢!

在Eclipse中,我使用自己的xml文件(“custom_dialog”)创建了一个自定义alertdialog,它被称为“usernamealert”。

如果用户尚未输入用户名(即username.length == 0),我希望弹出警报。

在这个布局中,我有一个textView(“你叫什么名字?”),editText和按钮(“usernameButton”)。

在为按钮添加onclicklistener之前,一切正常。这是我的(相关的)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();

当我把onclicklistener放进去的时候,它破了!我应该把它放在哪里?

(以下是我曾尝试过的......全部在我的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();



}
});

我说的代码之后:

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

再一次,它在我开始弄乱按钮之前就已经工作了!!

2 个答案:

答案 0 :(得分:9)

需要指定代码搜索按钮的位置,如果它只在主机的xml中搜索“findViewById”,则应该

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

这是我的班级Hireteachernegotiate.class的一部分,它有一个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();

这是对话框布局

<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>

答案 1 :(得分:2)

试试这个。

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

看看它是否有效,或者它是否有所帮助。