gpt4 book ai didi

java - 当edittext不为空时如何自动按回车键?

转载 作者:行者123 更新时间:2023-12-02 09:38:20 24 4
gpt4 key购买 nike

我正在创建一个 EditText 和从 Firebase 获得的值,如果 edittext 不为空,则可能会自动按下 Enter 键

private void init(){
Log.d(TAG, "init: initializing");

if (mSearchText != null){
what must i write here?
}

mSearchText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
if(actionId == EditorInfo.IME_ACTION_SEARCH
|| actionId == EditorInfo.IME_ACTION_DONE
|| keyEvent.getAction() == KeyEvent.ACTION_DOWN
|| keyEvent.getAction() == KeyEvent.KEYCODE_ENTER){

//execute our method for searching
geoLocate();
}

return false;
}
});

最佳答案

[1]您可以尝试下面的代码

String yourtext=mSearchText.getText().toString().trim();
if(!TextUtils.isEmpty(yourtext)) //or else you can use if(yourtext!=null && !yourtext.equalsIgnoreCase(""))
{
mSearchText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
if(actionId == EditorInfo.IME_ACTION_SEARCH
|| actionId == EditorInfo.IME_ACTION_DONE
|| keyEvent.getAction() == KeyEvent.ACTION_DOWN
|| keyEvent.getAction() == KeyEvent.KEYCODE_ENTER){

//execute your method for searching
geoLocate();
}

return false;
}
});
}

[2]否则你可以使用这个(直接方法调用)来实现你的功能

String yourtext=mSearchText.getText().toString().trim();
if(!TextUtils.isEmpty(yourtext)) //or else you can use if(yourtext!=null && !yourtext.equalsIgnoreCase(""))
{
//execute your method for searching
geoLocate();
}

关于java - 当edittext不为空时如何自动按回车键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57320390/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com