将焦点设置在EditText上

224

我有一个EditText字段,并为其设置了OnFocusChangeListener。当它失去焦点时,会调用一个方法,检查EditText的值与数据库中的值是否相同。如果该方法的返回值为true,则会显示一条消息并将焦点再次放回到EditText上。直到该方法的返回值为false时,焦点应始终保持在EditText上,键盘也应该显示。

编辑:我认为我尚未完全清楚我的真正问题:在EditText的值被编辑为使方法“checkLiganame(liganame)”返回false的值之前,屏幕上的其他项目都不应该能够进行编辑。只有EditText字段可以编辑。

这是我的代码(对我无效):

final EditText Liganame = (EditText) findViewById(R.id.liganame);

    Liganame.setOnFocusChangeListener(new OnFocusChangeListener() {

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (!hasFocus) {

                String liganame = Liganame.getText().toString();


                if (checkLiganame(liganame)) {
                    Toast toast = Toast.makeText(CreateTableActivity.this,
                            "Dieser Liganame ist bereits vergeben",
                            Toast.LENGTH_SHORT);
                    toast.show();
                    Liganame.requestFocus();
                }
            }

还有方法:

public boolean checkLiganame(String liganame) {
    boolean found = false;

    DatabaseHelper databaseHelper = new DatabaseHelper(this);
    SQLiteDatabase db = databaseHelper.getReadableDatabase();

    Cursor cursor = db.query("liga", new String[] { "liganame" },
            "liganame = '" + liganame + "'", null, null, null, null);
    Log.i("Liganame: ", String.valueOf(cursor));

    db.close();
    if (cursor != null) {
        found = true;
    }

    return found;
}
这段代码会产生以下结果:在EditText失去焦点后,焦点会跳回到EditText,但我无法再编辑文本了。
EDIT2:更改了我的代码。情境如下:
我点击第一个EditText并输入一个已经存在于数据库中的字符串。弹出toast提示。现在我不能再编辑我的字符串了。我点击键盘上的“下一步”按钮,焦点仍停留在第一个EditText上。我尝试编辑我的字符串,但没有任何反应。相反,我的新字符串显示在第二个EditText中。我点击设备的返回箭头,然后重新点击第一个和第二个EditText-->键盘不再显示。
这是我的新代码:
public class CreateTableActivity extends Activity implements
    OnFocusChangeListener {

private EditText Liganame, Mannschaftsanzahl;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.create_league);

    Liganame = (EditText) findViewById(R.id.liganame);
    Liganame.setOnFocusChangeListener(this);
    Mannschaftsanzahl = (EditText) findViewById(R.id.mannschaftsanzahl);
    Mannschaftsanzahl.setOnFocusChangeListener(this);

    final Button save_button = (Button) findViewById(R.id.create_tabelle_speichern_button);

    OnClickListener mCorkyListener = new OnClickListener() {
        public void onClick(View v) {
            ButtonClick();
        }
    };
    save_button.setOnClickListener(mCorkyListener);



}

@Override
public void onFocusChange(View v, boolean hasFocus) {
    String liganame = Liganame.getText().toString();

    if (checkLiganame(liganame)) {
        if (Liganame.requestFocus()) {
            getWindow()
                    .setSoftInputMode(
                            WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
            Mannschaftsanzahl.clearFocus();
            Toast.makeText(CreateTableActivity.this,
                    "Dieser Liganame ist bereits vergeben",
                    Toast.LENGTH_SHORT).show();
        }
    }
}

如果以下答案对您有所帮助,请接受其中一个答案。 - Akshatha S R
18个回答

4
    Button btnClear = (Button) findViewById(R.id.btnClear);

    EditText editText1=(EditText) findViewById(R.id.editText2);
    EditText editText2=(EditText) findViewById(R.id.editText3);

    btnClear.setOnClickListener(new View.OnClickListener() {

        @Override

        public void onClick(View v) {

            editText1.setText("");
            editText2.setText("");

            editText1.requestFocus();
        }
    });

3
 private void requestFocus(View view) {
        if (view.requestFocus()) {
            getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
        }
    }

//Function Call
requestFocus(yourEditetxt);

2

针对Xamarin.Android,我创建了这个扩展。

public static class ViewExtensions
{
    public static void FocusEditText(this EditText editText, Activity activity)
    {
        if (editText.RequestFocus())
        {
            InputMethodManager imm = (InputMethodManager)activity.GetSystemService(Context.InputMethodService);
            imm.ShowSoftInput(editText, ShowFlags.Implicit);
        }
    }
}

2
您可以用一行代码实现这个功能:
yourEditText.RequestFocusFromTouch();

实际上,这是唯一对我有效的答案。它模拟了我想要的触摸。 - Antonio Vlasic

2
如果在onCreate()中使用requestFocus()会导致键盘无法在点击时弹出,可以使用SingleLiveEvent并在其中请求焦点。以下是操作步骤:

BindingAdapter

@BindingAdapter("requestFocus")
fun bindRequestFocus(editText: EditText, event: Event<Boolean>?) {
    event?.getContentIfNotHandled()?.let {
        if (it) editText.requestFocus()
    }
}

1
请在清单文件上尝试此代码。
<activity android:name=".EditTextActivity" android:windowSoftInputMode="stateAlwaysVisible">
</activity>

1

我不知道您是否已经找到了解决方案,但是针对您在请求焦点后的编辑问题:

您尝试过在edittext1上调用selectAll()setSelection(0)(如果为空)方法吗?

请让我知道这是否有帮助,这样我就可以编辑我的答案以提供完整的解决方案。


1
new OnEditorActionListener(){
   @Override
   public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
      editText.requestFocus();
      //used ******* return true ******
      return **true**;
   }
} 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接