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>



然后上厕所...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值