editText中的键盘事件处理

city = (LinearLayout) findViewById(R.id.arrow_down);
        busTypeTv = (TextView) findViewById(R.id.tv_type_bus);
        cancle = (TextView) findViewById(R.id.tv_quxiao);
        perform = (TextView) findViewById(R.id.tv_wancheng);
        frameLayout = (FrameLayout) findViewById(R.id.frameLayout_id);
        second_pv = (PickerView) findViewById(R.id.second_pv);
        Log.i("tag", "second_pv==" + second_pv);
        lists_city = new ArrayList<String>();
        lists_bustype = new ArrayList<String>();
        Log.i("tag", "seconds==" + lists);
        city.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {                
                //1.得到InputMethodManager对象
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                //2.调用hideSoftInputFromWindow方法隐藏软键盘
                imm.hideSoftInputFromWindow(city.getWindowToken(), 0); //强制隐藏键盘
                frameLayout.setVisibility(View.VISIBLE);
                HelperUtils.addCitysDatas(lists_city);
                second_pv.setData(lists_city);
                second_pv.setOnSelectListener(new onSelectListener() {

                    @Override
                    public void onSelect(String text) {
                        cityTv.setText(text);
                    }
                });
            }
        });
        
        
        
        
        
        Android EditText 不弹出输入法总结

方法一:

在AndroidMainfest.xml中选择哪个activity,设置windowSoftInputMode属性为adjustUnspecified|stateHidden

例如:

    <activity android:name=".Main"
    android:label="@string/app_name"
    android:windowSoftInputMode="adjustUnspecified|stateHidden"
    android:configChanges="orientation|keyboardHidden">
    < intent-filter>
    < action android:name="android.intent.action.MAIN" />
    < category android:name="android.intent.category.LAUNCHER" />
    < /intent-filter>
    < /activity>  

方法二:

让EditText失去焦点,使用EditText的clearFocus方法

例如:

    EditText edit=(EditText)findViewById(R.id.edit);
    edit.clearFocus();  

方法三:

强制隐藏Android输入法窗口

例如:

    EditText edit=(EditText)findViewById(R.id.edit);
    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(edit.getWindowToken(),0);  

2.EditText始终不弹出软件键盘

例:

    EditText edit=(EditText)findViewById(R.id.edit);
    edit.setInputType(InputType.TYPE_NULL);  

研究了下android中焦点Focus和弹出输入法的问题。在网上看了些例子都不够全面,在这里全面总结下。

一:EditText为什么会默认弹出输入法?

同样的代码,碰到有EditText控件的界面时有的机子会弹出输入法,有的机子不会弹出。不好意思,这问题我也一头雾水,谁知道可以告诉我,否则我就把这个问题留下来,以后研究android 源码时再搞个清楚。但是...我有解决方案。

二:默认弹出和默认关闭输入法的解决方案。

1.默认关闭,不至于进入Activity就打开输入法,影响界面美观。

①在布局文件中,在EditText前面放置一个看不到的LinearLayout,让他率先获取焦点:

    <LinearLayout
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:layout_width="0px"
    android:layout_height="0px"/>  

②方法二:先看一个属性android:inputType:指定输入法的类型,int类型,可以用|选择多个。取值可以参考:android.text.InputType类。取值包括:text,textUri,

phone,number,等.

Android SDK中有这么一句话“If the given content type is TYPE_NULL then a soft keyboard will not be displayed for this text view”,

先将EditText的InputType改变为TYPE_NULL,输入法就不会弹出.然后再设置监听,再重新设置它的InputType.

    editText.setOnTouchListener(new OnTouchListener() {
                    public boolean onTouch(View v, MotionEvent event) {  
                        // TODO Auto-generated method stub  
                        int inType = editText.getInputType(); // backup the input type  
                        editText.setInputType(InputType.TYPE_NULL); // disable soft input      
                        editText.onTouchEvent(event); // call native handler      
                        editText.setInputType(inType); // restore input type     
                        return true;                      
                    }  
                });  

2.默认弹出。有时候按照需求可能要求默认弹出输入法。方案如下:

    EditText titleInput = (EditText) findViewById(R.id.create_edit_title);
    titleInput.setFocusable(true);
     titleInput.requestFocus();
     onFocusChange(titleInput.isFocused());
    private void onFocusChange(boolean hasFocus)
    {
    final boolean isFocus = hasFocus;
    (new Handler()).postDelayed(new Runnable() {
    public void run() {
    InputMethodManager imm = (InputMethodManager)
    titleInput.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
    if(isFocus)
    {
    imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
    }
    else
    {
    imm.hideSoftInputFromWindow(titleInput.getWindowToken(),0);
    }
    }
    }, 100);
    } 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值