<legend id='zIV02'><style id='zIV02'><dir id='zIV02'><q id='zIV02'></q></dir></style></legend>
        <bdo id='zIV02'></bdo><ul id='zIV02'></ul>
      <i id='zIV02'><tr id='zIV02'><dt id='zIV02'><q id='zIV02'><span id='zIV02'><b id='zIV02'><form id='zIV02'><ins id='zIV02'></ins><ul id='zIV02'></ul><sub id='zIV02'></sub></form><legend id='zIV02'></legend><bdo id='zIV02'><pre id='zIV02'><center id='zIV02'></center></pre></bdo></b><th id='zIV02'></th></span></q></dt></tr></i><div id='zIV02'><tfoot id='zIV02'></tfoot><dl id='zIV02'><fieldset id='zIV02'></fieldset></dl></div>
    1. <small id='zIV02'></small><noframes id='zIV02'>

    2. <tfoot id='zIV02'></tfoot>

      1. 如何使用 xml 布局获取对在自定义对话框中创建的

        时间:2023-08-30
        <legend id='tZ0Y7'><style id='tZ0Y7'><dir id='tZ0Y7'><q id='tZ0Y7'></q></dir></style></legend>

              <i id='tZ0Y7'><tr id='tZ0Y7'><dt id='tZ0Y7'><q id='tZ0Y7'><span id='tZ0Y7'><b id='tZ0Y7'><form id='tZ0Y7'><ins id='tZ0Y7'></ins><ul id='tZ0Y7'></ul><sub id='tZ0Y7'></sub></form><legend id='tZ0Y7'></legend><bdo id='tZ0Y7'><pre id='tZ0Y7'><center id='tZ0Y7'></center></pre></bdo></b><th id='tZ0Y7'></th></span></q></dt></tr></i><div id='tZ0Y7'><tfoot id='tZ0Y7'></tfoot><dl id='tZ0Y7'><fieldset id='tZ0Y7'></fieldset></dl></div>

              <small id='tZ0Y7'></small><noframes id='tZ0Y7'>

              <tfoot id='tZ0Y7'></tfoot>
                <tbody id='tZ0Y7'></tbody>

                <bdo id='tZ0Y7'></bdo><ul id='tZ0Y7'></ul>
                1. 本文介绍了如何使用 xml 布局获取对在自定义对话框中创建的按钮的引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

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

                  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.

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

                  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;
                  

                  比onCreate方法:

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

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

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

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

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

                  bSalvarCarro 为空.

                  the bSalvarCarro is null.

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

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

                  dialogCarro.setContentView(R.layout.novo_carro_dialog);
                  

                  是这个吗(novo_carro_dialog.xml):

                  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>
                  

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

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

                  这篇关于如何使用 xml 布局获取对在自定义对话框中创建的按钮的引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:创建自定义对话框时出现问题 下一篇:如何通过代码在 AlertDialog 上单击“确定"?

                  相关文章

                  最新文章

                2. <legend id='8T574'><style id='8T574'><dir id='8T574'><q id='8T574'></q></dir></style></legend>

                3. <tfoot id='8T574'></tfoot>
                  <i id='8T574'><tr id='8T574'><dt id='8T574'><q id='8T574'><span id='8T574'><b id='8T574'><form id='8T574'><ins id='8T574'></ins><ul id='8T574'></ul><sub id='8T574'></sub></form><legend id='8T574'></legend><bdo id='8T574'><pre id='8T574'><center id='8T574'></center></pre></bdo></b><th id='8T574'></th></span></q></dt></tr></i><div id='8T574'><tfoot id='8T574'></tfoot><dl id='8T574'><fieldset id='8T574'></fieldset></dl></div>
                      <bdo id='8T574'></bdo><ul id='8T574'></ul>
                  1. <small id='8T574'></small><noframes id='8T574'>