android - 自定义警报对话框 Android 中的 OnClickListener

标签 android button android-alertdialog

我是一个自学成才的初学者,感谢耐心等待!谢谢!

在 Eclipse 中,我用它自己的 xml 文件(“custom_dialog”)制作了一个自定义警报对话框,它被称为“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();
        }

再一次,在我开始弄乱按钮之前它就起作用了!!

最佳答案

需要指定代码将在哪里搜索按钮,如果它只是“findViewById”,它将在主机的 xml 中搜索,应该是

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

这是我的类(class) 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>

关于android - 自定义警报对话框 Android 中的 OnClickListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12168358/

相关文章:

android - 在Kotlin android项目中使用intellij-markdown

vb.net - MessageBox 与 YesNoCancel - 否和取消触发相同的事件

android - 自定义 AlertDialog 未显示

android - 传递 "null"作为 DialogInterface.OnClickLIstener 合法吗?

android - 抽屉导航未从方法 DrawerLayout.openDrawer(GravityCompat.START) 的左侧打开

android - 无法在 Eclipse 中启动 Android JUnit 测试。停留在 : Launching: Creating source locator

java - Android - 如何禁用搜索按钮,如何实现 onSearchRequested()?

android - 带有 9-Patch 背景的按钮不包含内容

java - 如何在进一步处理之前关闭对话框?

java - 创建java程序来运行ADB命令