我有一个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();
        }
    }
}

当前回答

你可以用一行来完成:

yourEditText.RequestFocusFromTouch();

其他回答

这是为我工作,设置焦点和显示键盘

EditText userNameText = (EditText) findViewById(R.id.textViewUserNameText);
userNameText.setFocusable(true);
userNameText.setFocusableInTouchMode(true);
userNameText.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(userNameText, InputMethodManager.SHOW_IMPLICIT);
 private void requestFocus(View view) {
        if (view.requestFocus()) {
            getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
        }
    }

//Function Call
requestFocus(yourEditetxt);

请求聚焦不足以显示键盘。

为了获得焦点并显示键盘,你可以这样写:

if(myEditText.requestFocus()) {
    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}

编辑:在添加checkLiganame方法后,向答案添加额外的信息。

在checkLiganame方法中,检查游标是否为空。游标将始终返回一个对象,因此检查null不做任何事情。但是问题出在db.close();

关闭数据库连接时,游标将失效,很可能为空。

因此,在获取值后关闭数据库。

而不是检查游标是否为空,你应该检查返回的行数是否大于0:if(cursor. getcount() > 0),然后将你的布尔值设置为true。

EDIT2:下面是一些如何使它工作的代码。 编辑3:对不起,我添加了错误的代码……,

首先,如果另一个EditText获得焦点,则需要清除焦点。这可以用myEditText.clearFocus()来完成。然后在你的onFocusChangeListener中,你不应该真正关心第一个EditText是否有焦点,所以onFocusChangeListener可以看起来像这样:

public class MainActivity extends Activity implements OnFocusChangeListener {
    private EditText editText1, editText2;

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

        editText1 = (EditText) findViewById(R.id.editText1);
        editText1.setOnFocusChangeListener(this);
        editText2 = (EditText) findViewById(R.id.editText2);
        editText2.setOnFocusChangeListener(this);
    }

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

        if(liganame.length() == 0) {
            if(editText1.requestFocus()) {
                getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
                editText2.clearFocus();
                Toast.makeText(MainActivity.this, "Dieser Liganame ist bereits vergeben", Toast.LENGTH_SHORT).show();
            }
        }
    }
}

将第一个检查if(liganame.length() == 0)替换为您自己的检查,那么它应该可以工作。 请注意,所有EditText视图都应该将它们的onFocusChangeListener设置为相同的侦听器,就像我在示例中所做的那样。

我的答案是

当我阅读官方文档时,我认为这是最好的答案,只是将视图传递给参数,如您的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)
    }
}

我不知道你是否已经找到了解决方案,但是对于你的编辑问题,再次请求聚焦:

您是否尝试在edittext1上调用方法selectAll()或setSelection(0)(如果是空的)?

请让我知道这是否有帮助,所以我会编辑我的答案,以一个完整的解决方案。