软键盘的显示与隐藏

本文介绍了一个简单的Android应用程序,用于演示如何在Android应用中手动显示和隐藏软键盘。通过三个按钮分别实现软键盘的切换、开启和关闭功能,并提供了完整的XML布局文件及Java代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参考:
Android : 隐藏软键盘
Android手动显示和隐藏软键盘方法总结

布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <Button
        android:id="@+id/btn1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="软键盘开关"
        android:textColor="@color/colorAccent"
        android:textSize="16sp" />

    <Button
        android:id="@+id/btn2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="开启软键盘"
        android:textColor="@color/colorAccent"
        android:textSize="16sp" />

    <Button
        android:id="@+id/btn3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="关闭软键盘"
        android:textColor="@color/colorAccent"
        android:textSize="16sp" />

    <EditText
        android:id="@+id/et1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:imeOptions="actionNext"
        android:inputType="number|numberDecimal"
        android:maxLength="4"
        android:selectAllOnFocus="true"
        android:textColor="#333333"
        android:textSize="15sp" />

</LinearLayout>

代码

/**
 * 开关软键盘
 */
public class MainActivityII extends AppCompatActivity {

    private EditText mEt1;
    private Button mBtn1;
    private Button mBtn2;
    private Button mBtn3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mainii);

        mEt1 = (EditText) findViewById(R.id.et1);
        mBtn1 = ((Button) findViewById(R.id.btn1));
        mBtn2 = ((Button) findViewById(R.id.btn2));
        mBtn3 = ((Button) findViewById(R.id.btn3));

        mBtn1.setOnClickListener(new View.OnClickListener() {//切换
            @Override
            public void onClick(View v) {
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null) {
                    imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
                }
            }
        });

        mBtn2.setOnClickListener(new View.OnClickListener() {//开
            @Override
            public void onClick(View v) {
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null) {
                    mEt1.requestFocus();
                    imm.showSoftInput(mEt1, 0);//可toggle关闭

//                    imm.showSoftInput(mEt1, InputMethodManager.SHOW_FORCED); 
                    //强制开启只能mBtn3关,不能mBtn1的toggle关闭
                }
            }
        });

        mBtn3.setOnClickListener(new View.OnClickListener() {//关
            @Override
            public void onClick(View v) {
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null) {
                    imm.hideSoftInputFromWindow(mEt1.getWindowToken(), 0);
                }
            }
        });
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值