如何强制焦点编辑文本 [英] how to force focus to edit text

查看:155
本文介绍了如何强制焦点编辑文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读到各地有关如何设置对象然而,要重点问题,我似乎无法找到任何的答案,我想做的事情。

使用焦点监听我也做了以下内容:

  Ehour.setOnFocusChangeListener(新OnFocusChangeListener(){

        @覆盖
        公共无效onFocusChange(查看arg0中,布尔ARG1){
            // TODO自动生成方法存根
            如果(!ARG1){
                如果(Ehour.getText()长度()< = 0){
                    Toast.makeText(getApplicationContext(),否小时进入,Toast.LENGTH_LONG).show();
                    日历nohour = Calendar.getInstance();
                    Ehour.setText(的NumberFormatter(nohour.get(Calendar.HOUR_OF_DAY)));
Ehour.setFocusable(真正的);
Ehour.requestFocus();


                }
            }
        }});
 

我试图从后续的职位建议:

我看到这篇文章,但使用这些建议也似乎不工作: <一href="http://stackoverflow.com/questions/2150656/how-to-set-focus-on-a-view-when-a-layout-is-created-and-displayed">How要创建并显示一个布局时,在视图上设置焦点?

我的目标是当编辑文本焦点丢了,我要搬回来,如果给定项是无效的。

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

  Ehour.setOnFocusChangeListener(新OnFocusChangeListener(){

        @覆盖
        公共无效onFocusChange(查看arg0中,布尔ARG1){
            // TODO自动生成方法存根
            如果(!ARG1){
                如果(Ehour.getText()长度()&LT; = 0){
                    Toast.makeText(getApplicationContext(),否小时进入,Toast.LENGTH_LONG).show();
                    日历nohour = Calendar.getInstance();
                    Ehour.setText(的NumberFormatter(nohour.get(Calendar.HOUR_OF_DAY)));

                    ((EditText上)为arg0).requestFocus();

                }
            }
        }});
 

**编辑**

:我的不是requestFocus()之前,甚至还添加以下行

 ((EditText上)为arg0).setFocusable(真正的);
 

*编辑**

这是这个职位的尝试:<一href="http://stackoverflow.com/questions/1555109/stop-edittext-from-gaining-focus-at-activity-startup">Stop从的EditText在活动启动获得焦点

以下变化: 在第一布置头的布局我说:

 机器人:ID =@ + ID / enterdata
机器人:descendantFocusability =beforeDescendants
机器人:focusableInTouchMode =真
 

然后在OnChangeFocusListener我这样做:

  Ehour.setOnFocusChangeListener(新OnFocusChangeListener(){

        @覆盖
        公共无效onFocusChange(查看arg0中,布尔ARG1){
            // TODO自动生成方法存根
            如果(!ARG1){
                如果(((EditText上)为arg0).getText()长度()&LT; = 0){
                    Toast.makeText(getApplicationContext(),否小时进入,Toast.LENGTH_LONG).show();
                    日历nohour = Calendar.getInstance();
                    ((EditText上)为arg0).setText(的NumberFormatter(nohour.get(Calendar.HOUR_OF_DAY)));
                    findViewById(R.id.enterdata).requestFocus();
                    ((EditText上)为arg0).setFocusable(真正的);
                    ((EditText上)为arg0).setFocusableInTouchMode(真正的);
                    ((EditText上)为arg0).requestFocus();

                }
            }
        }});
 

不过,我仍然得到同样的行为之前,即重点转到停留,我想重点转到容貌集中的方式,但在编辑文本,但它似乎无法使用,就好像它仍然是编辑文本等待集中。

*编辑**

:下面按计算器上的地方谁问的问题用户遇到同样的问题,因为我发现最近的文章

code

  Ehour.setOnFocusChangeListener(新OnFocusChangeListener(){

        @覆盖
        公共无效onFocusChange(查看arg0中,布尔ARG1){
            // TODO自动生成方法存根
            如果(!ARG1){
                如果(((EditText上)为arg0).getText()长度()&LT; = 0){

                    日历nohour = Calendar.getInstance();
                    ((EditText上)为arg0).setText(的NumberFormatter(nohour.get(Calendar.HOUR_OF_DAY)));
                    如果(((EditText上)为arg0).requestFocus()){
                         。getWindow()setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
                         Emin.clearFocus();
                         Toast.makeText(getApplicationContext(),否小时进入,Toast.LENGTH_LONG).show();
                    }


                }
            }
        }});
 

解决方案

我有同样的问题,这里的code,我来解决:

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

最好的问候,

I read around about the questions on how to set an object to be focused however I cannot seem to find either an answer to what I am trying to do.

Using the On Focus Listener I have done the following:

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


                }
            }
        }});

I have tried the suggestions from the follow posts:

I read this post but using these suggestions as well do not seem to work: How to set focus on a view when a layout is created and displayed?

My goal is that when the edit text focus is lost, I need to move back to it if the entry given is invalid.

Furthermore as per the suggestion given by Bill Mote I have also tried:

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

                }
            }
        }});

** EDIT ** I even added the below line before the requestFocus() :

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

* EDIT **

Tried from this post : Stop EditText from gaining focus at Activity startup

The following changes : In the layout at the first layout header I added :

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

Then in the OnChangeFocusListener I did this:

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

                }
            }
        }});

However I still get the same behaviour as before, the edit text that the focus goes to stays that way but the edit text that I want the focus to go to looks focused but it cannot seem to be used as though it is still waiting to be focused.

* EDIT **

Code below as per recent post found on stackoverflow where the user who asked the question is experiencing the same problem as me:

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


                }
            }
        }});

解决方案

I had this same issue, here the code that I used to solve:

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

Best Regards,

这篇关于如何强制焦点编辑文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆