我想在用户完成EditText中的输入时点击API。我们怎么做? 我只想要EditText。
答案 0 :(得分:2)
首先添加这些属性以编辑文本
android:imeOptions="actionDone"
android:singleLine="true"
然后在编辑文本上使用此代码以查找用户何时按下
edittext.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// If the event is a key-down event on the "enter" button
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER)) {
// Call API
return true;
}
return false;
}
});
OR 像这样使用onEditorAction:
edittext.setImeOptions(EditorInfo.IME_ACTION_DONE);
etSearch.setOnEditorActionListener(new TextView.OnEditorActionListener()
{
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
//call api
return true;
}
return false;
}
}
);
答案 1 :(得分:0)
检查这是否对您有帮助:
edittext1.addTextChangedListener(new TextWatcher()
{
@Override public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
}
@Override public void onTextChanged(CharSequence s, int start, int before, int count)
{
if(s.length()>0)
{
}
}
@Override public void afterTextChanged(Editable s)
{
//Text change done
}
});
答案 2 :(得分:0)
您需要将TextWatcher添加到Edittext
editText.addTextChangedListener(new TextWatcher() { @Override
public void beforeTextChanged(CharSequence charSequence, int
i, int i1, int i2) {
}
@Override public void onTextChanged(CharSequence charSequence, int
i, int i1, int i2) {
if(charSequence.length()>2)
{
//Write code for hitting API
} }
@Override public void afterTextChanged(Editable s) {
} });
并且在onTextChanged()方法中你可以编写你的逻辑来命中 API。我的意思是你可以修复一些字符长度,然后点击API;
答案 3 :(得分:0)
Thanks to all , i solve my problem this way
edtTo.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
switch( actionId ) {
case EditorInfo.IME_ACTION_DONE:
// clear editText focus and hide
edtTo.clearFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(edtFrom.getWindowToken(), 0);
Log.e("Debug","Hit Api ");
break;
}
return true;
}
});