Struggling to Launch Numeric keypad on activity start

假如想象 提交于 2019-12-11 04:23:04

问题


I have few TextView's in my activity, when activity starts i want to show soft numeric keypad or that activity should always show keypad.

I have tried:

  1. To set android:windowSoftInputMode="stateAlwaysVisible" in manifest for activity but didn't worked.
  2. Added this code in activity onCreate, it shows the alphanumeric keypad but not numeric.

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

Need help on this.


回答1:


I have tried this for an EditText in a dynamic way...The method would be the same for TextView also..When activity starts,give focus on that textView ,so that it can show u the keypad

    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:


Place edit text in your xml file as first child.

<?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:


To pop up a numeric keyboard on start of the activity i used following steps:

Created edit text field in layout as:

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

but since you need the key board to be poped without any edit text so give

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

In function onCreate() show soft keyboard

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

Most important is to give focus to edit text in onResume method.

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


来源:https://stackoverflow.com/questions/8061328/struggling-to-launch-numeric-keypad-on-activity-start

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!