[英]App button as keyboard press
I'm trying to map a button in my fragment layout as a keypress on a keyboard. 我试图将片段布局中的按钮映射为键盘上的按键。 This is my button press:
这是我的按钮按下:
Button down = (Button)(myView.findViewById(R.id.btnDOWN));
down.setOnTouchListener(new View.OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.i(TAG, "Down pressed");
return false;
}
});
I wasn't able to find any library which would allow me to do so, all I've found was reverse case, when a keyboard or controller press would be sent to device. 我找不到任何允许我这样做的库,当键盘或控制器的按键被发送到设备时,我发现的库都是相反的情况。
What I'm trying to generally do here, is an app that acts like a controller for RetroPie by using the phone as a bluetooth keyboard, with only the keys necessary for a specific controller. 我在这里通常尝试做的是一个应用程序,该应用程序通过将手机用作蓝牙键盘来充当RetroPie的控制器,仅使用特定控制器所需的键。
The method you're using is not correct. 您使用的方法不正确。 you should use the setOnClickListener :
您应该使用setOnClickListener :
Here an example from Android official guide : 这是来自Android官方指南的示例:
public class MyActivity extends Activity {
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.content_layout_id);
final Button button = findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
}
}
If you're looking for continuos aka long click, you should change it to setOnLongClickListener : 如果您要寻找连续的又名长按,则应将其更改为setOnLongClickListener :
down.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
return true;
}
});
It wasn't hard to find in this case the solution on this site 在这种情况下,不难在该站点上找到解决方案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.