android实现虚拟按键实例

来源:互联网 发布:小程序如何连接数据库 编辑:程序博客网 时间:2024/05/01 22:27
上代码: 
Java代码  收藏代码
  1. package com.tcl.testandroid;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.Instrumentation;  
  5. import android.os.Bundle;  
  6. import android.os.Handler;  
  7. import android.view.KeyEvent;  
  8. import android.view.View;  
  9. import android.view.View.OnClickListener;  
  10. import android.widget.Button;  
  11. import android.widget.EditText;  
  12. import android.widget.TextView;  
  13.   
  14. public class TestAndroidActivity extends Activity {  
  15.     private Handler mhHandler = new Handler();  
  16.     private Instrumentation in =new Instrumentation();  
  17.     /** Called when the activity is first created. */  
  18.     @Override  
  19.     public void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.         setContentView(R.layout.main);  
  22.         setTheme(android.R.style.Theme_Dialog);  
  23.         final Button button = (Button) findViewById(R.id.button1);  
  24.         final EditText editText = (EditText) findViewById(R.id.editText);  
  25.         button.setOnClickListener(new OnClickListener() {  
  26.               
  27.             @Override  
  28.             public void onClick(View v) {  
  29.                 editText.requestFocus();  
  30.                 new Thread(new Runnable() {  
  31.                       
  32.                     @Override  
  33.                     public void run() {  
  34.                         // TODO Auto-generated method stub  
  35.                         in.sendKeyDownUpSync(KeyEvent.KEYCODE_F);  
  36.                         in.sendKeyDownUpSync(KeyEvent.KEYCODE_U);  
  37.                         in.sendKeyDownUpSync(KeyEvent.KEYCODE_C);  
  38.                         in.sendKeyDownUpSync(KeyEvent.KEYCODE_K);  
  39.                         mhHandler.post(new Runnable() {  
  40.                               
  41.                             @Override  
  42.                             public void run() {  
  43.                                 // TODO Auto-generated method stub  
  44.                                 button.requestFocus();  
  45.                             }  
  46.                         });  
  47.                     }  
  48.                 }).start();  
  49.                   
  50.                 // TODO Auto-generated method stub  
  51.                   
  52.             }  
  53.         });  
  54.           
  55.           
  56.     }  
  57. }  


上layout 
Java代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical" android:layout_width="wrap_content"  
  4.     android:layout_height="wrap_content">  
  5.     <Button android:text="Button" android:id="@+id/button1"  
  6.         android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>  
  7.     <EditText android:id="@+id/editText" android:layout_height="wrap_content"  
  8.         android:layout_width="192dp" />  
  9. </LinearLayout>  

0 0
原创粉丝点击