【问题标题】:Struggling to Launch Numeric keypad on activity start努力在活动开始时启动数字键盘
【发布时间】:2011-12-25 01:39:41
【问题描述】:

我的活动中几乎没有 TextView,当活动开始时我想显示软数字键盘或该活动应始终显示键盘。

我试过了:

  1. 在清单中设置 android:windowSoftInputMode="stateAlwaysVisible" 以进行活动,但没有奏效。
  2. 在活动 onCreate 中添加了此代码,它显示字母数字键盘,但不显示数字。

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);

在这方面需要帮助。

【问题讨论】:

  • TextView 不是 EditText。我也尝试过使用 EditText,但是当我单击/触摸 EditText 时它会显示键盘,即使我设置了参数 android:focusable="true" android:focusableInTouchMode="true" android:inputType="phone" android:digits=" 0123456789".
  • 对于布局XML,可以设置android:numeric="integer"。

标签: android numeric-keypad


【解决方案1】:

我已经以动态方式尝试过 EditText ...... TextView 的方法也相同......当活动开始时,将焦点放在那个 textView 上,以便它可以向你显示键盘

    boolean checkFocus=EditText.requestFocus();
    Log.i("CheckFocus", ""+checkFocus);
    if(checkFocus==true)
    {
    InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    EditText.setInputType(InputType.TYPE_CLASS_PHONE); [select Input type as according to ur requirement]
    mgr.showSoftInput(EditText, InputMethodManager.SHOW_IMPLICIT);
    }

【讨论】:

    【解决方案2】:

    将编辑文本作为第一个子项放置在您的 xml 文件中。

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                 ............
                   .........>
     <EditText android:layout_width="0dp"
        android:layout_height="0dp"/>
    ..............
    ................
    </LinearLayout>
    

    【讨论】:

      【解决方案3】:

      要在活动开始时弹出数字键盘,我使用了以下步骤:

      layout 中创建了编辑文本字段:

      <EditText
              ...
              android:inputType="number"    
              ...  />
      

      但是因为你需要在没有任何编辑文本的情况下弹出键盘所以给

       <EditText
              ...
              android:layout_width="0dp"
              android:layout_height="0dp"
              android:inputType="number"    
              ...  />
      

      在函数onCreate()中显示软键盘

      InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
      imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
      

      最重要的是在onResume方法中重点编辑文本。

          @Override
          public void onResume() {
              super.onResume();
              editText.setFocusableInTouchMode(true);
              editText.requestFocus();
          }
      

      【讨论】:

        猜你喜欢
        • 2011-10-17
        • 1970-01-01
        • 2012-12-28
        • 2012-04-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多