啥也不说了,直接贴代码:
package org.winplus.keydemo; import android.app.Activity; import android.app.Instrumentation; import android.os.Bundle; import android.util.Log; import android.view.KeyEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.Window; import android.widget.Button; public class MenuDemoActivity extends Activity { protected static final String TAG = "MenuDemoActivity"; @Override public void onCreate(Bundle savedInstanceState) { requestWindowFeature(Window.FEATURE_NO_TITLE); super.onCreate(savedInstanceState); setContentView(R.layout.main); final Button button = (Button) findViewById(R.id.button1); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { sendKeyCode(KeyEvent.KEYCODE_DPAD_DOWN); } }); final Button button2 = (Button) findViewById(R.id.button2); button2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { sendKeyCode(KeyEvent.KEYCODE_DPAD_DOWN); } }); } /** * 传入需要的键值即可 * @param keyCode */ private void sendKeyCode(final int keyCode){ new Thread () { public void run() { try { Instrumentation inst = new Instrumentation(); inst.sendKeyDownUpSync(keyCode); } catch (Exception e) { Log.e("Exception when sendPointerSync", e.toString()); } } }.start(); } }
时间: 2024-11-20 12:56:38