• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

gisoracle

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

android 打开软键盘

android 打开软键盘
<LinearLayout 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:orientation="vertical" >
    <!-- 定义显示软键盘按钮 -->
    <Button
        android:id="@+id/btn_soft"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="弹出/关闭软键盘" />

</LinearLayout>
package com.example.yanlei.yl2;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;


public class MainActivity extends AppCompatActivity {
    // 定义软键盘的按钮
    private Button btnSoft;
    //声明InputMethodManager对象
    InputMethodManager imm ;
    //声明一个textview

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //取得InputMethodManager对象
        imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        //得到布局中的所有对象
        findView();
        //设置对象的监听器
        setListener();
    }

    private void findView() {
        // 得到布局中的所有对象
        btnSoft = (Button) findViewById(R.id.btn_soft);
    }
    //设置对象的监听器
    private void setListener() {
        btnSoft.setOnClickListener(listener);
    }
    OnClickListener listener=new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            //第一次调用显示,再次调用则隐藏,如此反复
            //触发软键盘,InputMethodManager.HIDE_NOT_ALWAYS能够正常的隐藏,除非遇到SHOW_FORCED.
            imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
        }
    };
}

 

posted on 2015-12-02 16:36  gisai  阅读(435)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3