【问题标题】:How to get reference to a Button created in a custom Dialog, using a xml layout?如何使用 xml 布局获取对在自定义对话框中创建的按钮的引用?
【发布时间】:2010-10-25 00:21:15
【问题描述】:

我有一个活动,用户可以在其中单击标签附近的按钮更新特定信息。这个按钮会触发一个对话框,其中我有一些字段来获取用户输入和一个按钮来完成编辑。

我的问题是我无法在对话框特定的 xml 布局中获得对按钮声明的引用。按钮引用返回 null。按照一些代码 sn-p 来说明。

触发事件以构建对话框的按钮在活动中声明为实例变量,如下所示:

private Button bConfigurarCarro;

比onCreate方法:

bConfigurarCarro = (Button)findViewById(R.id.bConfigurarCarro);
 bConfigurarCarro.setOnClickListener(configuraCarroListener);

这会正确触发事件以创建对话框:

protected OnClickListener configuraCarroListener = new OnClickListener(){
public void onClick(View v) {
showDialog(CARRO_DIALOG_ID);
Log.d(TAG, "Executando evento do botão de configuração de carro no abastecimento.");
            }
        };

比创建对话框重写 onCreateDialog 方法是这样的:

@Override
protected Dialog onCreateDialog(int id) {
  switch (id) {
    case TIME_DIALOG_ID:
        return new TimePickerDialog(this, mTimeSetListener, hora, minuto, false);
    case DATE_DIALOG_ID:
        return new DatePickerDialog(this, mDateSetListener, ano, mes, dia);
    case CARRO_DIALOG_ID:
        Log.d(TAG, "Criando dialog de cadastro de carro.");
        dialogCarro = new Dialog(this);
        dialogCarro.setContentView(R.layout.novo_carro_dialog);
        bSalvarCarro = (Button)findViewById(R.id.botaoSalvarCarro);
        bSalvarCarro.setOnClickListener(salvarCarroListener);
        dialogCarro.setTitle("Carro");
        dialogCarro.getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
        Log.d(TAG, "Dialog de cadastro de carro criado retornando...");
        return dialogCarro;
    }
        return null;
}

NullPointer 触发的特定行是在尝试获取上面的 Button 引用后,我尝试 setOnClickListener..

bSalvarCarro = (Button)findViewById(R.id.botaoSalvarCarro);
bSalvarCarro.setOnClickListener(salvarCarroListener);

bSalvarCarro 为空。

我尝试使用代码行在上面设置的对话框的 xml 布局:

dialogCarro.setContentView(R.layout.novo_carro_dialog);

是这个吗(novo_carro_dialog.xml):

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent" android:layout_height="fill_parent">
            <TableRow>
                <TextView android:id="@+id/marcaCarro"
                    android:layout_width="wrap_content" android:layout_height="wrap_content" 
                    android:text="@string/marcaCarro"/>
                <EditText android:id="@+id/tMarcaCarro" android:singleLine="true" android:layout_width="fill_parent"
                android:layout_height="wrap_content" android:width="130px"/>
            </TableRow>
            <TableRow>
                <TextView android:id="@+id/nomeCarro"
                    android:layout_width="wrap_content" android:layout_height="wrap_content" 
                    android:text="@string/nomeCarro"/>
                <EditText android:id="@+id/tNomeCarro" android:singleLine="true" android:layout_width="fill_parent"
                android:layout_height="wrap_content"/>
            </TableRow>
            <TableRow>
                <TextView android:id="@+id/anoCarro"
                    android:layout_width="wrap_content" android:layout_height="wrap_content" 
                    android:text="@string/anoCarro"/>
                <EditText android:id="@+id/tAnoCarro" android:singleLine="true" android:layout_width="fill_parent"
                android:numeric="integer" android:maxLength="4" android:layout_height="wrap_content" />
            </TableRow>
            <TableRow>
                <TextView android:id="@+id/modeloCarro"
                    android:layout_width="wrap_content" android:layout_height="wrap_content" 
                    android:text="@string/modeloCarro"/>
                <EditText android:id="@+id/tModeloCarro" android:singleLine="true" android:layout_width="fill_parent"
                android:layout_height="wrap_content" />
            </TableRow>
            <Button android:id="@+id/botaoSalvarCarro" android:layout_width="wrap_content"
                    android:layout_height="wrap_content" android:text="@string/bSalvarCarro" />
</TableLayout>

如您所见,Button 是用 id botaoSalvarCarro 声明的,但尝试获取对它的引用会返回 null。我对此有点困惑,好像我取出设置侦听器的行,对话框正确显示,所以。如何正确获取对该按钮的引用?

【问题讨论】:

    标签: android button reference dialog


    【解决方案1】:
    dialogCarro = new Dialog(this);
    dialogCarro.setContentView(R.layout.novo_carro_dialog);
    bSalvarCarro = (Button)dialogCarro.findViewById(R.id.botaoSalvarCarro);
    

    【讨论】:

    • 谢谢。有效。不敢相信我错过了,因为你可以想象我是 Android 新手。在 5 分钟内。我将能够将此标记为正确答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-07
    • 2015-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-23
    相关资源
    最近更新 更多