Android的 - 将焦点设置的EditText微调后的选择 [英] Android - set focus on EditText after Spinner selection

查看:232
本文介绍了Android的 - 将焦点设置的EditText微调后的选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在屏幕上显示以下项目:

  EditText上用的名字
微调性别选择(男|女)
电子邮件的EditText

当应用程序首次推出,我想重点就录名字的EditText进行设置。那么男或之后女已经在微调选择,
我想重点要在位于微调在电子邮件的EditText设置。

我用 setOnItemSelectedListener 设置 requestFocus的上的电子邮件的EditText,但问题是,它会自动设置重点
这个的EditText每当我启动应用程序。
这是因为通过默认喷丝显示第一选择在这种情况下是男性,因此它认为
一个选择已经作出,并将专注于电子邮件字段。

我不介意在默认情况下,微调已经被选中的第一选择,但如果我能以某种方式重写 requestFocus的要在设置
用名字的EditText最初将是巨大的。

XML:

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@ + ID / LinearLayout中
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:方向=垂直>   <的EditText
        机器人:ID =@ + ID / ForenameForm
        机器人:layout_width =285dp
        机器人:layout_height =65dp
        机器人:提示=@字符串/ forenameHint
        机器人:行=1
        机器人:单线=真
        机器人:TEXTSIZE =20SP>
    < /&的EditText GT;
   <微调
        机器人:ID =@ + ID / SpinnerGender
        机器人:spinnerMode =对话
        机器人:TEXTSIZE =30sp
        机器人:layout_width =285dp
        机器人:layout_height =60dp
        机器人:提示=@字符串/ spinnerGender
        机器人:项=@阵列/ genderList>    < /微调>   <的EditText
        机器人:ID =@ + ID / EmailForm
        机器人:layout_width =285dp
        机器人:layout_height =65dp
        机器人:提示=@字符串/ emailHint
        机器人:行=1
        机器人:单线=真
        机器人:TEXTSIZE =20SP>
    < /&的EditText GT;< / LinearLayout中>

活动类:

 保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);        最终的EditText用的名字=(EditText上)findViewById(R.id.ForenameForm);
        forename.requestFocus();        最终的EditText电子邮件=(EditText上)findViewById(R.id.EmailForm);        微调微调=(微调)findViewById(R.id.SpinnerGender);
        spinner.setFocusable(真);
        spinner.setFocusableInTouchMode(真);
        spinner.setOnItemSelectedListener(新AdapterView.OnItemSelectedListener()
        {            @覆盖
            公共无效onItemSelected(适配器视图<>为arg0,ARG1查看,
                    INT ARG2,长ARG3){
                email.requestFocus();
            }            @覆盖
            公共无效onNothingSelected(适配器视图<>为arg0){            }
        });
    }


解决方案

这个怎么样的解决方案。

 确定一个布尔标志,并设置它默认为false。在OnCreate中设置的forName的焦点在setOnItemSelectedListener
     如果标志为false然后设置标志真
     其他重点电子邮件

所以你的code会像

 布尔标志= FALSE;
保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);        最终的EditText用的名字=(EditText上)findViewById(R.id.ForenameForm);
        forename.requestFocus();        最终的EditText电子邮件=(EditText上)findViewById(R.id.EmailForm);        微调微调=(微调)findViewById(R.id.SpinnerGender);
        spinner.setFocusable(真);
        spinner.setFocusableInTouchMode(真);
        spinner.setOnItemSelectedListener(新AdapterView.OnItemSelectedListener()
        {            @覆盖
            公共无效onItemSelected(适配器视图<>为arg0,ARG1查看,
                    INT ARG2,长ARG3){
              //这里的变化
                如果(标志== FALSE)
                    标志=真实的;
                其他
                    email.requestFocus();
            }            @覆盖
            公共无效onNothingSelected(适配器视图<>为arg0){            }
        });
    }

I have the following items displayed on the screen:

EditText Forename
Spinner gender selection (Male | Female)
EditText Email

When the application is first launched, I want the focus to be set on the Forename EditText. Then after "Male" or "Female" has been selected in the Spinner, I want the focus to be set on the Email EditText that is located below the spinner.

I have used setOnItemSelectedListener to set the requestFocus on the email EditText, but the problem is that it automatically sets the focus on this EditText whenever I launch the application. This happens because by default the spinner displays the first selection which in this case is "Male" and therefore it thinks that a selection has already been made and sets the focus on the Email field.

I don't mind the first selection to be already selected in the spinner by default but if I could somehow override the requestFocus to be set on the Forename EditText initially that would be great.

XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout" 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

   <EditText
        android:id="@+id/ForenameForm"
        android:layout_width="285dp"
        android:layout_height="65dp"
        android:hint="@string/forenameHint"
        android:lines="1"
        android:singleLine="true"
        android:textSize="20sp" >
    </EditText>


   <Spinner  
        android:id="@+id/SpinnerGender" 
        android:spinnerMode="dialog"
        android:textSize="30sp" 
        android:layout_width="285dp"
        android:layout_height="60dp"
        android:prompt="@string/spinnerGender"  
        android:entries="@array/genderList">

    </Spinner>

   <EditText
        android:id="@+id/EmailForm"
        android:layout_width="285dp"
        android:layout_height="65dp"
        android:hint="@string/emailHint"
        android:lines="1"
        android:singleLine="true"
        android:textSize="20sp" >
    </EditText>

</LinearLayout>

Activity class:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final EditText forename=(EditText)findViewById(R.id.ForenameForm);
        forename.requestFocus();

        final EditText email=(EditText)findViewById(R.id.EmailForm);

        Spinner spinner=(Spinner)findViewById(R.id.SpinnerGender);
        spinner.setFocusable(true);
        spinner.setFocusableInTouchMode(true);   


        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
        {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                email.requestFocus();               
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {

            }               
        });  
    }

解决方案

How about this solution.

define a boolean flag and set it default false.

in oncreate set the focus of forname

in setOnItemSelectedListener 
     if flag is false then set flag true
     else focus email 

So your code will be like

boolean flag = false;
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final EditText forename=(EditText)findViewById(R.id.ForenameForm);
        forename.requestFocus();

        final EditText email=(EditText)findViewById(R.id.EmailForm);

        Spinner spinner=(Spinner)findViewById(R.id.SpinnerGender);
        spinner.setFocusable(true);
        spinner.setFocusableInTouchMode(true);   


        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
        {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
              // changes here
                if(flag == false)
                    flag = true;
                else 
                    email.requestFocus();               
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {

            }               
        });  
    }

这篇关于Android的 - 将焦点设置的EditText微调后的选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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