我有一个EditText-Field,并为它设置一个OnFocusChangeListener。当它失去焦点时,将调用一个方法,该方法用数据库中的值检查EditText的值。如果该方法的返回值为true,则显示toast,并且焦点应该再次回到EditText上。焦点应该总是回到EditText上,键盘应该显示出来,直到方法的返回值为false。

编辑:我认为,我还没有完全清楚我的真正问题:屏幕上没有其他项目应该能够编辑,直到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,但我不能再编辑文本了。

编辑2:更改了我的代码。场景:

我单击第一个EditText并在其中放入一个String,该String已经在数据库中。烤面包片露出来了。现在我不能编辑我的字符串了。我点击键盘上的“下一步”,焦点停留在第一个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();
        }
    }
}

当前回答

如果动态创建EditText,则必须设置如下所示的requestFocus()。

    EditText editText = new EditText(this);
    editText.setWidth(600);
    editText.requestFocus();

如果我们已经在xml视图中声明了组件,那么我们必须找到它,我们可以如下所示的焦点。

EditText e1=(EditText) findViewById(R.id.editText1);
e1.requestFocus();

它只将焦点设置为相应的EditText组件。

其他回答

Xamarin的。我已经创建了这个扩展。

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);
        }
    }
}

如果在onCreate()中使用requestFocus()引入了键盘不显示的问题,使用BindingAdapter使用SingleLiveEvent并请求其中的焦点。

以下是如何做到这一点:

BindingAdapter

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

如果动态创建EditText,则必须设置如下所示的requestFocus()。

    EditText editText = new EditText(this);
    editText.setWidth(600);
    editText.requestFocus();

如果我们已经在xml视图中声明了组件,那么我们必须找到它,我们可以如下所示的焦点。

EditText e1=(EditText) findViewById(R.id.editText1);
e1.requestFocus();

它只将焦点设置为相应的EditText组件。

我的答案是

当我阅读官方文档时,我认为这是最好的答案,只是将视图传递给参数,如您的EditText,但showSoftKeyboard似乎不能在横向上工作

private fun showSoftKeyboard(view: View) {
    if (view.requestFocus()) {
        val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT)
    }
}

private fun closeSoftKeyboard(view: View) {
    if (view.requestFocus()) {
        val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        imm.hideSoftInputFromWindow(view.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
    }
}

把这一行放在onCreate()上

editText.requestFocus();