我希望我的应用程序由红外遥控器控制。并且应用程序必须以kitkat版本运行。基本上我在这里有4个图像视图,我希望用户使用IR遥控器来移动和选择特定的图像。任何人都可以帮助我从哪里开始?我应该只选择一个特定的遥控器,还是所有遥控器上的遥控器都是相同的。
答案 0 :(得分:4)
终于找到了解决方案!!!! :) 以下是示例代码。
public class DpadActivity extends Activity {
private ImageView iv1,iv2;
private static Toast tst;
private int current_position=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dpad);
iv1=(ImageView)findViewById(R.id.imageView1);
iv2=(ImageView)findViewById(R.id.ImageView2);
iv1.requestFocus();
iv1.setBackgroundResource(R.drawable.hilight);
iv2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "iv2 clicked", Toast.LENGTH_LONG).show();
}
});
iv1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "iv1 clicked", Toast.LENGTH_LONG).show();
}
});
tst.makeText(getApplicationContext(), "Ha hai", Toast.LENGTH_LONG).show();
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (KeyEvent.KEYCODE_DPAD_CENTER == keyCode
|| KeyEvent.KEYCODE_ENTER == keyCode) {
tst.makeText(getApplicationContext(), "Enter Pressed", Toast.LENGTH_LONG).show();
return true;
} else if (KeyEvent.KEYCODE_DPAD_LEFT == keyCode) {
tst.makeText(getApplicationContext(), "Left Pressed", Toast.LENGTH_LONG).show();
return true;
} else if (KeyEvent.KEYCODE_DPAD_RIGHT == keyCode) {
tst.makeText(getApplicationContext(), "Right Pressed", Toast.LENGTH_LONG).show();
return true;
} else if (KeyEvent.KEYCODE_DPAD_UP == keyCode) {
if(current_position==1)
{
iv2.requestFocus();
current_position=2;
}else if(current_position==2)
{
iv1.requestFocus();
current_position=1;
}
tst.makeText(getApplicationContext(), "Up Pressed", Toast.LENGTH_LONG).show();
return true;
} else if (KeyEvent.KEYCODE_DPAD_DOWN == keyCode) {
if(current_position==1)
{
iv2.requestFocus();
current_position=2;
}else if(current_position==2)
{
iv1.requestFocus();
current_position=1;
}
tst.makeText(getApplicationContext(), "Down Pressed", Toast.LENGTH_LONG).show();
return true;
}
Toast.makeText(getApplicationContext(), "Nothing Detected", Toast.LENGTH_LONG).show();
return super.onKeyDown(keyCode, event);
}
}