如何强制将焦点聚集到编辑文本上

7

我看到了一些关于如何设置一个对象为焦点的问题,但是似乎没有找到我正在尝试做的事情的答案。

使用 On Focus Listener,我已经完成了以下操作:

  Ehour.setOnFocusChangeListener(new OnFocusChangeListener() {

        @Override
        public void onFocusChange(View arg0, boolean arg1) {
            // TODO Auto-generated method stub
            if (!arg1) {
                if (Ehour.getText().length()<=0) {
                    Toast.makeText(getApplicationContext(), "No Hour Entered", Toast.LENGTH_LONG).show();
                    Calendar nohour = Calendar.getInstance();
                    Ehour.setText(numberformatter(nohour.get(Calendar.HOUR_OF_DAY)));
Ehour.setFocusable(true);
Ehour.requestFocus();


                }
            }
        }});

我尝试了以下帖子中的建议: 我阅读了这篇文章,但是使用这些建议似乎也不起作用: 如何在创建和显示布局时将焦点设置在视图上? 我的目标是,当编辑文本失去焦点时,如果输入无效,则需要返回到它。
此外,根据Bill Mote的建议,我还尝试了:
 Ehour.setOnFocusChangeListener(new OnFocusChangeListener() {

        @Override
        public void onFocusChange(View arg0, boolean arg1) {
            // TODO Auto-generated method stub
            if (!arg1) {
                if (Ehour.getText().length()<=0) {
                    Toast.makeText(getApplicationContext(), "No Hour Entered", Toast.LENGTH_LONG).show();
                    Calendar nohour = Calendar.getInstance();
                    Ehour.setText(numberformatter(nohour.get(Calendar.HOUR_OF_DAY)));

                    ((EditText)arg0).requestFocus();

                }
            }
        }});

**编辑** 在调用requestFocus()之前,我甚至添加了以下代码行:

((EditText)arg0).setFocusable(true);

***编辑****

尝试从这篇文章开始:停止EditText在Activity启动时获得焦点

以下更改: 在第一个布局头部的布局中,我添加了:

android:id="@+id/enterdata"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"

然后在 OnChangeFocusListener 中我这样做:

Ehour.setOnFocusChangeListener(new OnFocusChangeListener() {

        @Override
        public void onFocusChange(View arg0, boolean arg1) {
            // TODO Auto-generated method stub
            if (!arg1) {
                if (((EditText)arg0).getText().length()<=0) {
                    Toast.makeText(getApplicationContext(), "No Hour Entered", Toast.LENGTH_LONG).show();
                    Calendar nohour = Calendar.getInstance();
                    ((EditText)arg0).setText(numberformatter(nohour.get(Calendar.HOUR_OF_DAY)));
                    findViewById(R.id.enterdata).requestFocus();
                    ((EditText)arg0).setFocusable(true);
                    ((EditText)arg0).setFocusableInTouchMode(true);
                    ((EditText)arg0).requestFocus();

                }
            }
        }});

然而,我仍然得到了与以前相同的行为,焦点所在的编辑文本仍然保持原样,但我希望焦点转移到的编辑文本看起来已经聚焦,但似乎无法像已经聚焦一样使用它,好像它仍在等待聚焦。

*** 编辑 ****

如下代码与最近在stackoverflow上发现的帖子相同,该用户遇到与我相同的问题:

 Ehour.setOnFocusChangeListener(new OnFocusChangeListener() {

        @Override
        public void onFocusChange(View arg0, boolean arg1) {
            // TODO Auto-generated method stub
            if (!arg1) {
                if (((EditText)arg0).getText().length()<=0) {

                    Calendar nohour = Calendar.getInstance();
                    ((EditText)arg0).setText(numberformatter(nohour.get(Calendar.HOUR_OF_DAY)));
                    if (((EditText)arg0).requestFocus()) {
                         getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
                         Emin.clearFocus();   
                         Toast.makeText(getApplicationContext(), "No Hour Entered", Toast.LENGTH_LONG).show();  
                    }


                }
            }
        }});

查看这篇帖子:https://dev59.com/iHI_5IYBdhLWcg3wBub4 我尝试了其中一个答案,我将把它添加到我的问题中。 - TimCS
我甚至尝试将所有编辑文本框的硬编码设置为清除焦点,并将setFocusable和setFocusableInTouchMode设置为false,但行为仍然相同,看起来我想要焦点的编辑文本已经聚焦,但任何输入都会出现在用户将要移动到的编辑文本中!!!救命啊!!! - TimCS
在这篇帖子中:https://dev59.com/j2Yq5IYBdhLWcg3woCC7?rq=1,用户似乎遇到了同样的问题,并且看起来并没有解决。不过,我将尝试这里的建议,看看它们是否适用于我。 - TimCS
我已经尝试了上面帖子中提供的建议,但对情况没有任何影响。我会添加代码到问题中以展示我所尝试的。 - TimCS
3个回答

3

尝试使用arg0.requestFocus()

不确定View是否自带requestFocus(),所以你可能需要进行类型转换...((EditText) arg0).requestFocus()


抱歉,我应该在我的问题中表明,我已经按照您的建议尝试了,但结果与之前提到的相同。 - TimCS
1
这两个编辑文本框的行为对我来说似乎有些奇怪,因为我想要移回的那个看起来是聚焦的,并且似乎有一个光标在它上面,但是焦点已经移动到的那个也看起来一样,即使我“点击”回到我想要使用的那个编辑文本框,它改变的却不是这个。 - TimCS
在我看來,似乎現在沒有逃避這個問題的方法。因此,我必須嘗試在接受記錄時進行驗證檢查,而不是在用戶離開字段時進行檢查。這是針對Android 2.2的,所以我不確定它是否擴展到4。 - TimCS

1
我遇到了同样的问题,这是我用来解决的代码:
EditText editText = (EditText) findViewById(R.id.myTextViewId);
editText.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);

最好的问候,


1
以下是在 Kotlin 中从片段中执行此操作的方法: activity?.let { val imm = it.getSystemService(Activity.INPUT_METHOD_SERVICE) as? InputMethodManager imm?.let { manager -> manager.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT) } } - Anna Harrison

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

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