Android中EditText 设置 imeOptions 无效问题的解决方法

有时候我们需要在EditText  输出完之后 需要在键盘出现 右下角变成“Go”或“前往 搜索时;通常我们需要设置Android:imeOptions属性。Android:imeOptions的值有actionGo、 actionSend 、actionSearch、actionDone等

但是今天我发现设置了无效  那是因为我设置了 android:maxLines="1"

解决方法

就是去掉 android:maxLines="1"  设置 android:singleLine="true" 有必要还需要 inputType设置为text

网上有一种监听点击回车 搜索的写法 这种写法 会执行两次 

解决方法

editText.setOnEditorActionListener(new TextView.OnEditorActionListener() { 
public boolean onEditorAction(TextView v,int actionId,KeyEvent event) {             
if (actionId==EditorInfo.IME_ACTION_SEND ||(event!=null&&event.getKeyCode()== KeyEvent.KEYCODE_ENTER)) 
{        
//do something;       
return true;       
}        
return false;      
}    
});

解决方法

是 1  (ps 这种方法我感觉写法有点多余)

public boolean onEditorAction(TextView v,KeyEvent event) { 
    //以下方法防止两次发送请求 再判断动作 
    if (actionId == EditorInfo.IME_ACTION_SEND || 
        (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) { 
      switch (event.getAction()) { 
        case KeyEvent.ACTION_UP: 
          //发送请求 
          String keyWord = et_search.getText().toString().trim(); 
          if (null == keyWord) 
            keyWord = ""; 
          dismisspopup(); 
          LogUtils.d("向服务器发送搜索请求:" + keyWord); 
          //发起查询 
          searchByKeyWord(keyWord); 
          hideSoftInput(); 
          return true; 
        default: 
          return true; 
      } 
    } 
    return false; 
  } 

还有一种写法   直接监听actionId等于搜需要的时间即可

EditText editText = (EditText) contentView.findViewById(R.id.editText); 
    editText.setOnEditorActionListener(new OnEditorActionListener() { 
      @Override 
      public boolean onEditorAction(TextView v,KeyEvent event) { 
        if (actionId == EditorInfo.IME_ACTION_SEARCH) { 
          Toast.makeText(getActivity(),"1111111",Toast.LENGTH_SHORT).show(); 
        } 
        return false; 
      } 
    }); 

以上所述是小编给大家介绍的Android中EditText 设置 imeOptions 无效问题的

解决方法

,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程小技巧网站的支持!

以上是来客网为你收集整理的解决方法

">Android中EditText 设置 imeOptions 无效问题的

解决方法

全部内容,希望文章能够帮你解决解决方法

">Android中EditText 设置 imeOptions 无效问题的

解决方法

所遇到的程序开发问题。

如果觉得来客网网站内容还不错,欢迎将来客网网站推荐给程序员好友。