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

Android软键盘用法实例

时间:2015-09-11 17:37:55      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:android   package   public   键盘   import   

package com.example.key;




import android.os.Bundle;

import android.app.Activity;

import android.content.Context;

import android.view.Menu;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.view.View.OnClickListener;

import android.view.inputmethod.InputMethodManager;


public class MainActivity extends Activity {


 @Override

 protected void onCreate(Bundle savedInstanceState) { 

   super.onCreate(savedInstanceState); 

   setContentView(R.layout.activity_main); 

   final EditText ed2 = (EditText) findViewById(R.id.editText1);

   Button b1 = (Button) findViewById(R.id.button1); 

   b1.setOnClickListener(new OnClickListener() { 

     @Override

     public void onClick(View v) { 

       ed2.requestFocus(); 

       show(ed2); 

     } 

   });

   Button b2 = (Button) findViewById(R.id.button2);

   b2.setOnClickListener(new OnClickListener() {

     @Override

     public void onClick(View v) {

       toggle();

     }

   });

 }

 private void show(EditText ed2) {

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

   imm.showSoftInput(ed2, InputMethodManager.SHOW_IMPLICIT);

 }

 private void toggle() {

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

   imm.toggleSoftInput(0, 0);

 }

 @Override

 public boolean onCreateOptionsMenu(Menu menu) {

   // Inflate the menu; this adds items to the action bar if it is present.

   getMenuInflater().inflate(R.menu.main, menu);

   return true;

 }

}


Android软键盘用法实例

标签:android   package   public   键盘   import   

原文地址:http://7213105.blog.51cto.com/7203105/1693821

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