6

我阅读了有关如何设置要聚焦的对象的问题,但是我似乎找不到任何我想要做的事情的答案。

使用 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();  
                    }


                }
            }
        }});
4

3 回答 3

3

试试arg0.requestFocus()

不知道我头顶上是否有View工具,所以你可能不得不把它扔掉......requestFocus()((EditText) arg0).requestFocus()

于 2013-02-09T13:16:20.590 回答
1

我有同样的问题,这里是我用来解决的代码:

EditText editText = (EditText) findViewById(R.id.myTextViewId);
editText.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);

此致,

于 2015-11-21T13:40:04.873 回答
0
new OnEditorActionListener(){
   @Override
   public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
      editText.requestFocus();
      //used ******* return true ******
      return **true**;
   }
} 
于 2015-07-31T05:09:57.490 回答