开发者

How to stop Soft keyboard showing automatically when focus is changed (OnStart event)

开发者 https://www.devze.com 2023-02-15 02:49 出处:网络
I am developing for a tablet using Android 2.2. I have a form which I am using for new and editable instances.When editable, I want to stop the user from editing certain fields. I manage this in my o

I am developing for a tablet using Android 2.2.

I have a form which I am using for new and editable instances. When editable, I want to stop the user from editing certain fields. I manage this in my onStart-event, setting the txt.setFocusableInTouchMode(false). This forces the focus to the next focusable EditText in my form (which is great), but when running, the soft keyboard automatically appears for the EditText with the focus. Anybody know how to stop this?

Here's the code (called in the onStart event):

private void PopulateFields(){
    TextView txtTitle = (TextView) this.findViewById(R.id.txtEquipmentEditTitle);
    AutoCompleteTextView txtMake = (AutoCompleteTextView) this.findViewById(R.id.autEquipmentEditMake);
    AutoCompleteTextView txtModel = (AutoCompleteTextView) this.findViewById(R开发者_如何学Python.id.autEquipmentEditModel);
    EditText txtDesc = (EditText) this.findViewById(R.id.txtEquipmentEditDesc);
    TextView txtDeptKey = (TextView) this.findViewById(R.id.txtEquipmentEditDeptKey);
    EditText txtSerial = (EditText) this.findViewById(R.id.txtEquipmentEditSerialNo);
    EditText txtBarCode = (EditText) this.findViewById(R.id.txtEquipmentEditBarCode);

    txtTitle.setText("Edit Equipment");
    txtMake.setText(make);
    txtModel.setText(model);
    txtDesc.setText(desc);
    txtDeptKey.setText(Integer.toString(deptKey));
    txtSerial.setText(serial);
    txtBarCode.setText(barCode);
    txtMake.setEnabled(false);
    txtModel.setEnabled(false);
    txtDesc.setEnabled(false);
    txtMake.setClickable(false);
    txtMake.setFocusableInTouchMode(false);
    txtModel.setFocusableInTouchMode(false);
    txtDesc.setFocusableInTouchMode(false);
    txtMake.setFocusable(false);
    txtModel.setFocusable(false);
    txtDesc.setFocusable(false);
}


Maybe this will help you:

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

Put this method in the onCreate() method of your activity when the activity is first created or called.


We can Hide the device Keybord by following ways which totally depends on the need of situation_

First Way_

EditText userId;
    /*
     * When you want that Keybord not shown untill user clicks on one of the EditText Field.
     */
    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

Second Way_ InputMethodManager

    /*
     * When you want to use service of 'InputMethodManager' to hide/show keyboard
     */
    InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

    inputManager.hideSoftInputFromWindow(userId.getWindowToken(),
            InputMethodManager.HIDE_NOT_ALWAYS);

Third Way_

    /*
     * Touch event Listener
     * When you not want to open Device Keybord even when user clicks on concern EditText Field.
     */
    userId.setOnTouchListener(new OnTouchListener(){
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            int inType = userId.getInputType(); // backup the input type
            userId.setInputType(InputType.TYPE_NULL); // disable soft input
            userId.onTouchEvent(event); // call native handler
            userId.setInputType(inType); // restore input type
            userId.setFocusable(true);
            return true; // consume touch even
        }
    });

Behalf of all of the above ways_ you can also define windowSoftInputMode in applicationsmanifest` file_

<manifest ... >
 <application ... >
    <activity 
        android:windowSoftInputMode="stateHidden|stateAlwaysHidden"
        ... >
        <intent-filter>
           ...
        </intent-filter>
    </activity>

 </application>
</manifest>

I hope this will help all!


If you want this for a particular EditText this will work.

editText.setShowSoftInputOnFocus(false);


this is the answer: In your AndroidManifest.xml add an attribute to the Activity:

        <activity android:name=".MyActivity" android:windowSoftInputMode="adjustPan"> </activity>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号