android - 自定义对话框中EditText的值

标签 android dialog

我有 ListActivity ,每个项目的 onClick 都会出现一个 Custom DialogCustom Dialog 包含微调器和 EditText现在我无法获得 EditText 的值,而 EditText 的调试值显示为空白或“”。

 protected void onListItemClick(ListView l, View v, int position, long id) 
    {
    super.onListItemClick(l, v, position, id);
    positionList=position;

    HashMap<String,Object>temp =(HashMap<String ,Object>)list.get(position);

    LayoutInflater inflater = (LayoutInflater) CommonScreen6.this.getSystemService(LAYOUT_INFLATER_SERVICE);
    View layout = inflater.inflate(R.layout.answer_screen,null);

    AlertDialog.Builder alertDialog=new AlertDialog.Builder(CommonScreen6.this);
    alertDialog.setTitle("Select Option");
    alertDialog.setView(layout);

    Spinner answerList=(Spinner)layout.findViewById(R.id.spinnerAnswerList);

    ArrayAdapter<String> adapterAnswerType = new ArrayAdapter<String> (CommonScreen6.this, android.R.layout.simple_spinner_item,(ArrayList<String>)temp.get(Constants.answerDesc));

    adapterAnswerType.setDropDownViewResource(android.R.layout.select_dialog_singlechoice);

    answerList.setAdapter(adapterAnswerType);            
    answerList.setOnItemSelectedListener(this);

    ArrayList<String> answerDescList=(ArrayList<String>)temp.get(Constants.answerDesc);

    answerList.setSelection(answerDescList.indexOf(temp.get(Constants.defaultAnswerDesc)));

    alertDialog.setPositiveButton("Submit",new DialogInterface.OnClickListener() 
      {

    @Override
    public void onClick(DialogInterface dialog, int which)
               {
      LayoutInflater inflater = (LayoutInflater) CommonScreen6.this.getSystemService(LAYOUT_INFLATER_SERVICE);
      View layout = inflater.inflate(R.layout.answer_screen,null);
      EditText editText=(EditText)layout.findViewById(R.id.remark);
      String remark=editText.getText().toString();

      HashMap<String,Object>temp =(HashMap<String ,Object>)list.get(positionList);
            temp.put(Constants.remark, remark);
            adapter.notifyDataSetChanged();
           }
  });
 }

最佳答案

您没有获得任何值的原因是因为您在读取值之前重新填充布局,从而将值重置为默认值。 我想您希望在提供给 onListItemClick 方法的 v 参数上查找 ViewById,而不是重新展开布局。

也就是代替

LayoutInflater inflater = (LayoutInflater) CommonScreen6.this.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.answer_screen,null);
EditText editText=(EditText)layout.findViewById(R.id.remark);

尝试类似的东西

EditText editText=(EditText)v.findViewById(R.id.remark);

您可能需要将 v 变量设置为最终变量,以使其正常工作。 我希望这会有所帮助。

关于android - 自定义对话框中EditText的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8398375/

相关文章:

android - 我无法更改我的 AlertDialog 主题

javascript - 你如何测试模态 jquery 对话框是否失去了焦点?

android - 可以在 android 的 edittext 中添加语音操作按钮吗

android - 英特尔 x86 仿真器加速器(HAXM 安装程序)修订版 6.0.5 显示与 Windows 不兼容

android - 如何检查 VideoView 是否包含视频?

android - Gradle .execute 方法在 gradle 更新后显示错误

java - 如何从小部件本身手动调用 `onUpdate`

c++ - 如何正确发送 CMFCMenuButton 选择的 ID 到消息映射?

Android - 如何在 native 屏幕上显示对话框?

c++ - 我的关于窗口打不开