android实现虚拟按键实例

上代码:
package com.tcl.testandroid;

import android.app.Activity;
import android.app.Instrumentation;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class TestAndroidActivity extends Activity {
	private Handler mhHandler = new Handler();
	private Instrumentation in =new Instrumentation();
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        setTheme(android.R.style.Theme_Dialog);
        final Button button = (Button) findViewById(R.id.button1);
        final EditText editText = (EditText) findViewById(R.id.editText);
        button.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				editText.requestFocus();
				new Thread(new Runnable() {
					
					@Override
					public void run() {
						// TODO Auto-generated method stub
						in.sendKeyDownUpSync(KeyEvent.KEYCODE_F);
						in.sendKeyDownUpSync(KeyEvent.KEYCODE_U);
						in.sendKeyDownUpSync(KeyEvent.KEYCODE_C);
						in.sendKeyDownUpSync(KeyEvent.KEYCODE_K);
						mhHandler.post(new Runnable() {
							
							@Override
							public void run() {
								// TODO Auto-generated method stub
								button.requestFocus();
							}
						});
					}
				}).start();
				
				// TODO Auto-generated method stub
				
			}
		});
        
        
    }
}


上layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:layout_width="wrap_content"
	android:layout_height="wrap_content">
	<Button android:text="Button" android:id="@+id/button1"
		android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
	<EditText android:id="@+id/editText" android:layout_height="wrap_content"
		android:layout_width="192dp" />
</LinearLayout>



然后上厕所...

你可能感兴趣的:(android)