2 ooomiao oOOMIAO 于 2013.02.03 11:14 提问

AlertDialog检索用户信息

我想要获取AlertDialog中的text字符串,没实现:

private void showAddSectorDlg() {
   if (BuildConfig.DEBUG) {
           Log.i(Constants.TAG_ACTSECTORS, "showAddSectorDialog() called.");
   }
   LayoutInflater inflater = getLayoutInflater();
   new AlertDialog.Builder(ActSectors.this)
   .setTitle(R.string.menu_new_sector)
   .setMessage(R.string.dlg_sect_add_msg)
   .setView(inflater.inflate(R.layout.dlg_sector_add, null))
   .setPositiveButton(R.string.dlg_save,
           new DialogInterface.OnClickListener() {
               public void onClick(
                       DialogInterface dialog,
                       int whichButton) {
                    EditText mNewSectorName = (EditText) findViewById(R.id.dlg_edt_nsector);   
                            //NullPointerException happens here:
                    String val = mNewSectorName.getText().toString();
                       if (BuildConfig.DEBUG) {
                        Log.i(Constants.TAG_ACTSECTORS, "New Sector Name: "+val);
                       }
                       grabVal(val);
                       dialog.dismiss();
                   }
           })
   .setNegativeButton(R.string.dlg_cancel,
           new DialogInterface.OnClickListener() {
               public void onClick(
                       DialogInterface dialog,
                       int whichButton) {
               }
           }).create().show();
   }

   private void grabVal(String newSector){
   mNSN = newSector;
   callCtlr(Constants.R_DB_ADDSECT);
   }

请高手帮忙。谢谢

2个回答

baimy1985
baimy1985   2013.02.04 10:20
已采纳
inflater.inflate(R.layout.dlg_sector_add, null)把这个动作放在`new AlertDialog.Builder`之前, 类似`
final View v = li.inflate(R.layout.edit_1, null);`

记得前面要加上final,然后再setview,后面的方法就没什么区别了

ce_la_vie
ce_la_vie   2013.02.03 13:11

要是获取AlertDialog的EditText值,修改代码如下:

 LayoutInflater inflater = getLayoutInflater();
  View view=inflater.inflate(R.layout.dlg_sector_add,null);
   new AlertDialog.Builder(ActSectors.this)
   .setTitle(R.string.menu_new_sector)
   .setMessage(R.string.dlg_sect_add_msg)
   .setView(view)
   //... your code here

然后用视图访问EditText:

EditText mNewSectorName = (EditText)view.findViewById(R.id.dlg_edt_nsector);
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!