【问题标题】:Android custom dialog onclick listener errorAndroid自定义对话框onclick监听器错误
【发布时间】:2018-03-25 19:59:59
【问题描述】:

我正在尝试在对话框窗口上实现自己的布局。但我不知道如何在点击列表器上创建,因为我在另一个类中定义了对话框,它有另一个布局。我如何从另一个布局的另一个类中的对话框 xml 中“找到”按钮。

这里是代码:

public void dialog(String text){
    final String karta = text;
    AlertDialog.Builder mBuilder = new AlertDialog.Builder(Terminal.this);
    View view = getLayoutInflater().inflate(R.layout.activity_dialog, null);
    mBuilder.setView(view);
    AlertDialog dialog = mBuilder.create();
    dialog.show();

    b1 = (Button) findViewById(R.id.b1);
    b1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            String akcia = "Prichod";
            pridajZaznam(karta, akcia);
        }
    });

    b2 = (Button) findViewById(R.id.b2);
    b2.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            String akcia = "Odchod prestavka";
            pridajZaznam(karta, akcia);
        }
    });

    b3 = (Button) findViewById(R.id.b3);
    b3.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            String akcia = "Odchod";
            pridajZaznam(karta, akcia);
        }
    });

    b4 = (Button) findViewById(R.id.b4);
    b4.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            String akcia = "Prichod prestavka";
            pridajZaznam(karta, akcia);
        }
    });
}

这个方法在类终端里面有自己的布局,但对话框使用activity_dialog.xml布局文件。如果我不使用侦听器,对话框会正确显示,它们会导致空指针异常...

感谢您的任何回答!

【问题讨论】:

  • 你在哪里声明按钮 b1、b2 ...
  • 在类中,也许我应该在对话框方法中声明它们?
  • View view = getLayoutInflater().inflate(R.layout.activity_dialog, null); 该视图包含来自activity_dialog的视图层次结构

标签: android layout onclick dialog listener


【解决方案1】:

尝试b1 = (Button) view.findViewById(R.id.b1); 而不是b1 = (Button) findViewById(R.id.b1);

【讨论】:

  • 乐于助人:)
【解决方案2】:

这是创建自定义对话框的方法 1. 像这样创建布局

<LinearLayout
 xmls:https://....
<TextView 
android:layout_height = "wrap_content"
android:layout_width= "match_parent"
android:id="@+id/myText/>
<LinearLayout
android:layout_height = "wrap_content"
android:layout_width= "match_parent"
android:orientation = "horizontal"
>
<Button
android:layout_height = "wrap_content"
android:layout_width= "match_parent"
android:id="@+id/positiveBtn/>
Button
android:layout_height = "wrap_content"
android:layout_width= "match_parent"
android:id="@+id/negativeBtn/>
</LinearLayout>

然后从你的活动中调用它

Dialog myDialog = new Dialog(context);
myDialog.setContentView(R.layout.your_dilog_layout);
Button positiveBtn  = myDialog.findViewById(R.id.positiveBtn);
Button negativeBtn= myDialog.findViewById(R.id.negativeBtn);
TextView tv= myDialog.findViewById(R.id.myText);
myText.setText("WWhat you want");
negativeBtn.setOnClickListenr(new OnClickListener....);
 myDialog.show();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-14
    相关资源
    最近更新 更多