码迷,mamicode.com
首页 > 移动开发 > 详细

android 打开软键盘

时间:2015-12-02 18:12:46      阅读:111      评论:0      收藏:0      [点我收藏+]

标签:

<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);
        }
    };
}

 

android 打开软键盘

标签:

原文地址:http://www.cnblogs.com/gisoracle/p/5013420.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!