如何获得参考自定义对话框中创建一个按钮,使用XML布局?自定义、创建一个、如何获得、布局

2023-09-07 04:20:38 作者:过往年少

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

我的问题是,我没能获得一个引用按钮申报的对话框特定的XML布局。该按钮参考返回null。遵循一些code片段来ilustrate。

按钮,触发建立对话事件被声明为活性的实例变量如下:

 专用按钮bConfigurarCarro;
 

比onCreate方法:

  bConfigurarCarro =(按钮)findViewById(R.id.bConfigurarCarro);
 bConfigurarCarro.setOnClickListener(configuraCarroListener);
 

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

 保护OnClickListener configuraCarroListener =新OnClickListener(){
公共无效的onClick(视图v){
的ShowDialog(CARRO_DIALOG_ID);
Log.d(TAG,Executando evento做波涛去configuração日卡罗没有abastecimento。);
            }
        };
 
wincc如何实现自定义对话框登陆 退出 干货

而不是创建对话框覆盖onCreateDialog方法是这样的:

  @覆盖
受保护的对话框onCreateDialog(INT ID){
  开关(ID){
    案例TIME_DIALOG_ID:
        返回新TimePickerDialog(这一点,mTimeSetListener,霍拉,minuto,假);
    案例DATE_DIALOG_ID:
        返回新DatePickerDialog(这一点,mDateSetListener,ANO,MES,直径);
    案例CARRO_DIALOG_ID:
        Log.d(TAG,Criando对话框德cadastro日卡罗。);
        dialogCarro =新的对话框(本);
        dialogCarro.setContentView(R.layout.novo_carro_dialog);
        bSalvarCarro =(按钮)findViewById(R.id.botaoSalvarCarro);
        bSalvarCarro.setOnClickListener(salvarCarroListener);
        dialogCarro.setTitle(卡罗);
        。dialogCarro.getWindow()的setLayout(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
        Log.d(TAG对话框德cadastro日卡罗克里亚retornando ......);
        返回dialogCarro;
    }
        返回null;
}
 

在这里的空指针触发的具体线路是一个在那里试图让上面的按钮引用后,我不是试图setOnClickListener ..

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

在bSalvarCarro为空。

在对话框的XML布局,我尝试设置上面用code线:

  dialogCarro.setContentView(R.layout.novo_carro_dialog);
 

这是一(novo_carro_dialog.xml):

 < TableLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
        机器人:layout_width =FILL_PARENT机器人:layout_height =FILL_PARENT>
            <的TableRow>
                < TextView的机器人:ID =@ + ID / marcaCarro
                    机器人:layout_width =WRAP_CONTENT机器人:layout_height =WRAP_CONTENT
                    机器人:文本=@字符串/ marcaCarro/>
                < EditText上机器人:ID =@ + ID / tMarcaCarro机器人:单线=真正的机器人:layout_width =FILL_PARENT
                机器人:layout_height =WRAP_CONTENT机器人:宽=130px/>
            < /的TableRow>
            <的TableRow>
                < TextView的机器人:ID =@ + ID / nomeCarro
                    机器人:layout_width =WRAP_CONTENT机器人:layout_height =WRAP_CONTENT
                    机器人:文本=@字符串/ nomeCarro/>
                < EditText上机器人:ID =@ + ID / tNomeCarro机器人:单线=真正的机器人:layout_width =FILL_PARENT
                机器人:layout_height =WRAP_CONTENT/>
            < /的TableRow>
            <的TableRow>
                < TextView的机器人:ID =@ + ID / anoCarro
                    机器人:layout_width =WRAP_CONTENT机器人:layout_height =WRAP_CONTENT
                    机器人:文本=@字符串/ anoCarro/>
                < EditText上机器人:ID =@ + ID / tAnoCarro机器人:单线=真正的机器人:layout_width =FILL_PARENT
                机器人:数字=整数的Andr​​oid版本:最大长度=4的Andr​​oid版本:layout_height =WRAP_CONTENT/>
            < /的TableRow>
            <的TableRow>
                < TextView的机器人:ID =@ + ID / modeloCarro
                    机器人:layout_width =WRAP_CONTENT机器人:layout_height =WRAP_CONTENT
                    机器人:文本=@字符串/ modeloCarro/>
                < EditText上机器人:ID =@ + ID / tModeloCarro机器人:单线=真正的机器人:layout_width =FILL_PARENT
                机器人:layout_height =WRAP_CONTENT/>
            < /的TableRow>
            <按钮机器人:ID =@ + ID / botaoSalvarCarro机器人:layout_width =WRAP_CONTENT
                    机器人:layout_height =WRAP_CONTENT机器人:文本=@字符串/ bSalvarCarro/>
< / TableLayout>
 

正如你可以看到按钮是ID为botaoSalvarCarro声明,但试图让一个参考吧返回null。我有点被这个困惑,如果我拿出这台监听器对话框正确显示该行,就这样。我如何引用这​​个按钮是否正确?

解决方案

  dialogCarro =新的对话框(本);
dialogCarro.setContentView(R.layout.novo_carro_dialog);
bSalvarCarro =(按钮)dialogCarro.findViewById(R.id.botaoSalvarCarro);
 

I have an Activity in which user can update a specific information clicking in a button near a label. This buttons than triggers a Dialog where I have some fields to get user input and a button to finish editing.

My problem is that I am not able to get a reference to the button declare in the dialog specific xml layout. The button reference returns null. Follow some code snippet to ilustrate.

Button which fires the event to build the dialog is declared as a instance variable in the activity as follows:

private Button bConfigurarCarro;

than onCreate method:

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

this correctly fires the event to create the dialog:

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.");
            }
        };

than to create the dialog Overrides onCreateDialog method like this:

@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;
}

The specific line where the NullPointer triggers is the one where after trying to get Button reference above, I than try to setOnClickListener..

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

the bSalvarCarro is null.

The xml layout for the Dialog which I try to set above using the line of code:

dialogCarro.setContentView(R.layout.novo_carro_dialog);

Is this one(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>

As you can see the Button is declared with the id botaoSalvarCarro, but trying to get a reference to it returns null. I am a bit confused by this as if I take out the line which sets the listener the dialog is correctly showed, so. How do I get the reference to this button correctly?

解决方案

dialogCarro = new Dialog(this);
dialogCarro.setContentView(R.layout.novo_carro_dialog);
bSalvarCarro = (Button)dialogCarro.findViewById(R.id.botaoSalvarCarro);