如何强制重点编辑文本

Tim*_*mCS 5 android focus android-edittext

我读到了关于如何设置一个对象的问题,但我似乎无法找到我想要做的答案.

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


                }
            }
        }});
Run Code Online (Sandbox Code Playgroud)

我试过以下帖子的建议:

我阅读这篇文章,但使用这些建议似乎也不起作用: 如何在创建和显示布局时将焦点设置在视图上?

我的目标是当编辑文本焦点丢失时,如果给出的条目无效,我需要返回到它.

此外,根据比尔莫特的建议,我也尝试过:

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

                }
            }
        }});
Run Code Online (Sandbox Code Playgroud)

**编辑**我甚至在requestFocus()之前添加了以下行:

((EditText)arg0).setFocusable(true);
Run Code Online (Sandbox Code Playgroud)

***编辑****

尝试从这篇文章:停止EditText获得焦点在活动启动

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

android:id="@+id/enterdata"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
Run Code Online (Sandbox Code Playgroud)

然后在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();

                }
            }
        }});
Run Code Online (Sandbox Code Playgroud)

然而,我仍然得到与以前相同的行为,焦点所依据的编辑文本保持这种方式,但我希望焦点去的编辑文本看起来很集中,但它似乎无法使用,好像它仍在等待成为聚焦.

***编辑****

下面的代码根据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();  
                    }


                }
            }
        }});
Run Code Online (Sandbox Code Playgroud)

Bil*_*ote 3

尝试arg0.requestFocus()

不确定是否能从我的头顶上View实现,所以你可能不得不铸造它......。requestFocus()((EditText) arg0).requestFocus()