Android控制软键盘的弹出和隐藏(1)


/**

 * 软键盘开关

 */

public void toggleKeyboard(View view) {

    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

    imm.toggleSoftInputFromWindow(view.getWindowToken(), 0, 0);

}

弹出键盘的时候是一个道理,想要编辑哪个EditText,要先让其获取焦点

  • 效果图

P3

P4

测试类

===


package com.kongqw.kqwkeysetdemo;



import android.app.Activity;

import android.content.Context;

import android.os.Bundle;

import android.view.View;

import android.view.inputmethod.InputMethodManager;

import android.widget.EditText;



public class MainActivity extends Activity {



    private EditText editText;

    private EditText editText2;



    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);



        editText = (EditText) findViewById(R.id.edittext);

        editText2 = (EditText) findViewById(R.id.edittext2);

    }



    /**

     * 弹出软键盘

     */

    public void openKeyboard(View view) {

        // 获取焦点

        editText2.setFocusable(true);

        editText2.setFocusableInTouchMode(true);

        editText2.requestFocus();

        // 弹出软键盘

        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

        imm.showSoftInput(editText2, 0);

    }



    /**

     * 关闭软键盘

     */

    public void closeKeyboard(View view) {

        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);

    }



    /**

     * 软键盘开关

     */

    public void toggleKeyboard(View view) {

        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

        imm.toggleSoftInputFromWindow(view.getWindowToken(), 0, 0);

    }

}

页面布局

====


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context=".MainActivity">



    <EditText

        android:id="@+id/edittext"

        android:layout_width="match_parent"

        android:layout_height="wrap_content" />



    <EditText

        android:id="@+id/edittext2"

        android:layout_width="match_parent"

        android:layout_below="@+id/edittext"

        android:layout_height="wrap_content" />



    <Button

        android:id="@+id/button"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_below="@+id/edittext2"

        android:onClick="openKeyboard"

        android:text="弹出软键盘" />



    <Button

        android:id="@+id/button2"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_below="@+id/button"

        android:onClick="closeKeyboard"

        android:text="关闭软键盘" />


### 尾声

如果你想成为一个优秀的 Android 开发人员,请集中精力,对基础和重要的事情做深度研究。

对于很多初中级Android工程师而言,想要提升技能,往往是自己摸索成长,不成体系的学习效果低效漫长且无助。 整理的这些架构技术希望对Android开发的朋友们有所参考以及少走弯路,本文的重点是你有没有收获与成长,其余的都不重要,希望读者们能谨记这一点。

这里,笔者分享一份从架构哲学的层面来剖析的视频及资料分享给大家梳理了多年的架构经验,筹备近6个月最新录制的,相信这份视频能给你带来不一样的启发、收获。![](https://img-blog.csdnimg.cn/img_convert/be5a1a74af799a8b3f8809e95acb3a61.webp?x-oss-process=image/format,png)

PS:之前因为秋招收集的二十套一二线互联网公司Android面试真题 (含BAT、小米、华为、美团、滴滴)和我自己整理Android复习笔记(包含Android基础知识点、Android扩展知识点、Android源码解析、设计模式汇总、Gradle知识点、常见算法题汇总。)

![](https://img-blog.csdnimg.cn/img_convert/115b54e24f745f0de2f08fe096502247.webp?x-oss-process=image/format,png)

#### 架构篇

**《Jetpack全家桶打造全新Google标准架构模式》**
![](https://img-blog.csdnimg.cn/img_convert/62a0fc7ec456d39eda8efd6f1159e3ef.webp?x-oss-process=image/format,png)




**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化学习资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618156601)**

**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

30091807)]




**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化学习资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618156601)**

**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值