【问题标题】:Android - Dialog Button onClick -> EditText ValueAndroid - 对话框按钮 onClick -> EditText 值
【发布时间】:2013-03-26 21:04:11
【问题描述】:

我的选项菜单中有一个“搜索”按钮,它显示了一个带有搜索掩码的对话框。 因此,当您单击“搜索”按钮时,它应该会开始实际搜索。

我在 onCreateDialog 中尝试做的是:

Button search_button = (Button) dialog.findViewById(R.id.d_search_button);
search_button.setOnClickListener(new View.OnClickListener() {
  public void onClick(View v) {
    EditText text = (EditText) v.findViewById(R.id.d_search_text);
  }
});

由于 text 为空,哪个不起作用,那么访问搜索文本的最佳方法是什么?

【问题讨论】:

    标签: android dialog onclick android-edittext


    【解决方案1】:
    final EditText text  = (EditText) v.findViewById(R.id.d_search_text);
    
       search_button.setOnClickListener(new View.OnClickListener() {
       public void onClick(View v) {
    
       String searchString = text.getText().toString();
        //do something
       }
    
    });
    

    【讨论】:

    • 我现在觉得有点傻,本来可以这么想的。大声笑
    【解决方案2】:

    使EditText text 成为最终的全局变量。并在设置事件时设置它,然后在事件触发时可用:

    Button search_button = (Button) dialog.findViewById(R.id.d_search_button);
    text = (EditText) v.findViewById(R.id.d_search_text);
    search_button.setOnClickListener(new View.OnClickListener() {
      public void onClick(View v) {
      //get your text here
      }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多