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

当前回答

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

其他回答

把这一行放在onCreate()上

editText.requestFocus();

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

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

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

如果你试图在布局膨胀之前调用requestFocus(),它将返回false。这段代码在布局膨胀后运行。您不需要如上所述的200毫秒延迟。

editText.post(Runnable {
   if(editText.requestFocus()) {
       val imm = editText.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager?
       imm?.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0)
   }
})

当点击按钮时,这将改变EditText的焦点:

public class MainActivity extends Activity {
    private EditText e1,e2;
    private Button b1,b2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        e1=(EditText) findViewById(R.id.editText1);
        e2=(EditText) findViewById(R.id.editText2);
        e1.requestFocus();
        b1=(Button) findViewById(R.id.one);
        b2=(Button) findViewById(R.id.two);
        b1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                e1.requestFocus();

            }
        });
        b2.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                e2.requestFocus();
            }
        });
    }
}

你可以用一行来完成:

yourEditText.RequestFocusFromTouch();