软键盘使用

来源:互联网 发布:搜狗五笔输入法 mac 编辑:程序博客网 时间:2024/05/22 04:44

运行效果:


功能:

1、点击回复按钮弹出软键盘并带有(EditText)

2、点击回复区域其他地方软键盘收缩,并隐藏EditText

2、点击back按钮,隐藏软键盘和EditText

代码:

public class MainActivity extends Activity {    private EditText etSend;    private InputMethodManager imm;    private MyLinearlayout layout1;//自定义的一个线性布局    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE |                WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);        setContentView(R.layout.activity_main);        Button btTest = (Button) findViewById(R.id.btTest);        etSend = (EditText) findViewById(R.id.etSend);        layout1 = (MyLinearlayout) findViewById(R.id.layoutSend);//覆盖在主布局上的layout        imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);//获得软键盘服务        //线性布局的点击事件        layout1.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                //点击隐藏                if (layout1.getVisibility() == View.VISIBLE) {                    layout1.setVisibility(View.GONE);                }                if (imm.isActive()) {                    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);//隐藏软键盘                }            }        });        btTest.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                //点击回复按钮 弹出发送框                layout1.setVisibility(View.VISIBLE);//设置布局可见                etSend.requestFocus();//给EditText设置焦点                imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);//弹出软键盘            }        });    }}
xml布局:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">    <LinearLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:orientation="horizontal">        <Button            android:id="@+id/btTest"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="回复" />    </LinearLayout>    <com.zuoni.keyboarddemo.MyLinearlayout        android:id="@+id/layoutSend"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:orientation="vertical"        android:visibility="gone">        <LinearLayout            android:id="@+id/layout1111"            android:layout_width="match_parent"            android:layout_height="0dp"            android:layout_weight="1"            android:orientation="horizontal" />        <LinearLayout            android:layout_width="match_parent"            android:layout_height="50dp"            android:gravity="center"            android:orientation="horizontal">            <EditText                android:id="@+id/etSend"                android:layout_width="0dp"                android:layout_height="40dp"                android:layout_weight="1"                android:background="#c2c1c1" />            <Button                android:layout_width="wrap_content"                android:layout_height="45dp"                android:text="发送" />        </LinearLayout>    </com.zuoni.keyboarddemo.MyLinearlayout></FrameLayout>
自定义的线性布局:

public class MyLinearlayout extends LinearLayout {    public MyLinearlayout(Context context) {        super(context);    }    public MyLinearlayout(Context context, AttributeSet attrs) {        super(context, attrs);    }    public MyLinearlayout(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);    }    //重写back事件    @Override    public boolean dispatchKeyEventPreIme(KeyEvent event) {        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {            if (MyLinearlayout.this.getVisibility() == VISIBLE) {                MyLinearlayout.this.setVisibility(GONE);            }        }        return super.dispatchKeyEventPreIme(event);    }}
实现原理:

1.在原来布局上方覆盖一层带有回复按钮的布局(使用FrameLayout)并设置为隐藏

2.点击回复按钮时候,将覆盖的布局显示出来

3.给“覆盖布局”设置点击事件,点击就隐藏自己并收回软键盘


为什么要自定义线性布局:


由于按back的时候,命令先被软键盘所拦截了,所以不能直接在MainActivity中拦截back事件

因此自定义线性布局,重写dispatchKeyEventPreIme 方法这样就可以按back键的时候让软键盘和布局同时接收事件(执行隐藏代码)


关键点:


要在set ContentView之前设置:

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

否则会出现EditText下方部分会被软键盘覆盖

0 0