EditText的焦点【转】
学习笔记1:进入Activity时不自动获取EditText的焦点,EditText失去焦点时关闭软键盘 ...
热度 1 ||
1:进入Activity时不自动获取EditText的焦点
 
 
 
在EditText的上一级也可以不是上一级的LinearLayout,RelativeLayout等等上面添加属性【android:focusable="true"   android:focusableInTouchMode="true"】
如:<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/answer_add"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
   android:background="#f0f0f0"
   android:focusable="true"
        android:focusableInTouchMode="true"
        android:orientation="vertical" > 这样做的原因是将焦点转移到其他控件中。当然你想转移到哪个控件就可以在哪个控件下设置这个属性。
2:EditText失去焦点时关闭软键盘
      有时候我们需要这种体验:点击EditText时弹出软键盘,点击其他位置时软键盘自动关闭。
      这个需要完成第1布操作之后。在调用setOnTouchListener方法。
      answer_add.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
answer_add.setFocusable(true);
answer_add.setFocusableInTouchMode(true);
answer_add.requestFocus();
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(edit_answer.getWindowToken(),0);
return false;
}
});
      其中
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(edit_answer.getWindowToken(),0);就是关闭软键盘的方法。
这个很容易懂的,就是当我点击这个区域的时候获取焦点,并强制关闭软键盘
3:弹出输入法的时候界面变形的处理
    在AndroidManifest.xml中的你要设置的Activity中添加
android:windowSoftInputMode="stateAlwaysHidden|adjustResize" 这个属性,其中windowSoftInputMode还有其他属性,百度一下,你就知道
   如:
<activity android:name=".RecordingActivity" 
 android:configChanges="keyboardHidden|orientation" 
 android:screenOrientation="portrait" 
 android:theme="@style/Theme.HalfTranslucent" 
 android:windowSoftInputMode="stateAlwaysHidden|adjustResize" />
Ok了。希望自己能坚持将每天的学习用笔记记录下来。
4:软键盘不挤压布局
 android:windowSoftInputMode="adjustPan" 
 
                    
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号