当我旋转 Samsung Galaxy Watch 4 上的表圈时,我试图捕捉动作,但它从未“运行”。
这是我的代码:
findViewById(R.id.Badtunna).setOnGenericMotionListener(new View.OnGenericMotionListener() {
@Override
public boolean onGenericMotion(View view, MotionEvent motionEvent) {
System.out.println("GUM");
if(motionEvent.getAction() == MotionEvent.ACTION_SCROLL &&
motionEvent.isFromSource(InputDeviceCompat.SOURCE_ROTARY_ENCODER)) {
System.out.println("BUBBEL");
return true;
}
return false;
}
});
我的视图是普通的TextView:
<TextView
android:id="@+id/Badtunna"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Badtunna" />
这是我在 MainActivity 中的 onCreate()
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
StrictMode.ThreadPolicy policy = new
StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
// Add all the domoticz devices
domoticz_devices.put(766, "idx_766");
domoticz_devices.put(17, "idx_17");
findViewById(R.id.Skymningssensor).requestFocus();
findViewById(R.id.Skymningssensor).setOnGenericMotionListener(new View.OnGenericMotionListener() {
@Override
public boolean onGenericMotion(View view, MotionEvent motionEvent) {
System.out.println("GUM");
if(motionEvent.getAction() == MotionEvent.ACTION_SCROLL &&
motionEvent.isFromSource(InputDeviceCompat.SOURCE_ROTARY_ENCODER)) {
System.out.println("BUBBEL");
return true;
}
return false;
}
});
TextView idx_17_button = findViewById(R.id.idx_17);
idx_17_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
toggleDomoticzDevice(17, "idx_17");
} catch (IOException e) {
e.printStackTrace();
}
}
});
TextView idx_657_button = findViewById(R.id.idx_657);
idx_657_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
toggleDomoticzDevice(657, "idx_657");
} catch (IOException e) {
e.printStackTrace();
}
}
});
handler.postDelayed(new Runnable() {
@Override
public void run() {
for (Integer i : domoticz_devices.keySet()) {
getDomoticzDeviceAndSetTextview(i, domoticz_devices.get(i));
}
handler.postDelayed(this, delay);
}
}, delay);
}
当焦点“工作”时键盘是否应该弹出? 我还更新了我的 TextView 并添加 focusable:true 和 focusableintouchmode:true
提前致谢!
最诚挚的问候麦克斯
文档在这里
https://developer.android.com/training/wearables/user-input/rotary-input#custom-scrolling
可能的问题是焦点
请记住确保您的视图获得焦点,否则事件将无法完成。
https://developer.android.com/training/wearables/user-input/rotary-input#custom-scrolling
默认情况下,启动 Activity 甚至点击视图不会为其提供焦点,即使它是可聚焦的。为了给你的视图焦点,视图必须使用标签或手动调用 View.requestFocus()。
例如,如果您要用于
Recyclerview
。
将以下属性添加到您的
recyclerview
XML 标记。
focusable="true"
focusableInTouchMode="true"
那就试试
setOnGenericMotionListener
,一定会成功的。
在 Java 或 kotlin 类的代码中使用以下
recyclerView.requestFocus()
recyclerView.requestFocusFromTouch()
在 XML 中使用此代码
焦点=“真”
focusableInTouchMode =“true”