【问题标题】:EditText request focusEditText 请求焦点
【发布时间】:2011-12-26 00:55:58
【问题描述】:

我正在设计一个登录页面:

UserName:  .....

Password:  .....

     LoginButton

当活动开始时,我希望焦点转到“用户名”文本框并出现键盘。

我正在使用以下代码:

    boolean checkFocus=user.requestFocus();
    Log.i("CheckFocus", ""+checkFocus);
    if(checkFocus==true)
    {
    InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    mgr.showSoftInput(user, InputMethodManager.SHOW_IMPLICIT);
    }

我不明白在哪里编写此代码以使键盘在活动开始时出现并且焦点位于“用户名”编辑文本框上。谁能指导一下?

【问题讨论】:

    标签: android android-layout android-edittext


    【解决方案1】:

    以编程方式:

    edittext.requestFocus();
    

    通过xml:

    <EditText...>
        <requestFocus />
    </EditText>
    

    或者手动调用 onClick 方法。

    【讨论】:

    • 没有在您声明编辑文本的布局 XML 中。stackoverflow.com/questions/2743559/…
    • 你在哪个设备上测试??有时,即使在活动开始时聚焦后,Android O/S 也不会打开键盘。这经常发生在小屏幕设备中。实际上 o/s 智能地检测屏幕大小并决定是否显示键盘。。您可以尝试的另一件事是手动调用编辑文本框的 onclick 侦听器...例如:editText .onClick(null);并检查 onClick 列表以处理空事件(即防止应用程序崩溃..)
    • 我在 HTC 的欲望上测试它。
    • 不,那仍然不起作用..你告诉我一件事,当我的活动开始时,首先它会创建 xml 文件并使用 setcontentView(),
    【解决方案2】:

    是的,我得到了答案。只需将manifest 文件编辑为:

            <activity android:name=".MainActivity"
            android:label="@string/app_name"
            android:windowSoftInputMode="stateAlwaysVisible" />
    

    并将EditText.requestFocus() 设置为onCreate()..

    谢谢..

    【讨论】:

    • 这应该是公认的答案,这是唯一对我有用的东西!谢谢队友
    【解决方案3】:

    youredittext.requestFocus()从活动中调用它

    oncreate();
    

    并在那里使用上面的代码

    【讨论】:

    • user=(EditText)findViewById(R.id.editText1); pass=(EditText)findViewById(R.id.editText2);
    • 我做了..但它不起作用..我已经在这些行之后附加了我的代码
    【解决方案4】:

    它对我的工作如下。

    ed1.requestFocus();
    
                return; //Faça um return para retornar o foco
    

    【讨论】:

      【解决方案5】:

      edittext.requestFocus() 在我的ActivityFragment 中为我工作

      【讨论】:

        【解决方案6】:
        >>you can write your code like
        
          if (TextUtils.isEmpty(username)) {
                    editTextUserName.setError("Please enter username");
                    editTextUserName.requestFocus();
                    return;
                }
        
                if (TextUtils.isEmpty(password)) {
                    editTextPassword.setError("Enter a password");
                    editTextPassword.requestFocus();
                    return;
                }
        

        【讨论】:

          【解决方案7】:

          设置为Manifest中的Activity:

          android:windowSoftInputMode="adjustResize"
          

          当视图准备好时设置焦点:

          fun setFocus(view: View, showKeyboard: Boolean = true){
              view.post {
                  if (view.requestFocus() && showKeyboard)
                      activity?.openKeyboard() // call extension function
              }
          }
          

          【讨论】:

            【解决方案8】:

            禁用软键盘(仅启用外部键盘),我通过在 EditText 上移动光标来修复它:

            editText.setSelection(editText.getText().length)
            

            【讨论】:

              猜你喜欢
              • 2013-07-09
              • 2019-07-08
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2017-02-14
              • 2012-02-27
              相关资源
              最近更新 更多