アクティビティ内でフォーカスされているビューがあるかどうか、およびそれがどのビューであるかを確認する必要があります。これを行う方法?
回答:
getCurrentFocus()アクティビティを呼び出します。
activity?.currentFocus
アクティビティのソースから:
/**
* Calls {@link android.view.Window#getCurrentFocus} on the
* Window of this Activity to return the currently focused view.
*
* @return View The current View with focus or null.
*
* @see #getWindow
* @see android.view.Window#getCurrentFocus
*/
public View getCurrentFocus() {
return mWindow != null ? mWindow.getCurrentFocus() : null;
}
何らかの理由で、getCurrentFocus()メソッドは使用できなくなりました。おそらくそれはすでに非推奨です、ここで実用的な代替手段:
View focusedView = (View) yourParentView.getFocusedChild();
getFocusedChild()メソッドViewGroupです。
代わりにこれを試してください。すべてをaの中に入れてthread、IDとクラス名をライブでに出力してくださいlogcat。このコードをの中に入れActivityて、onCreateメソッドに入れてから、logcat現在フォーカスいるものしてください。
new Thread(() -> {
int oldId = -1;
while (true) {
View newView= this.getCurrentFocus();
if (newView!= null && newView.getId() != oldId) {
oldId = view.getId();
String idName = "";
try {
idName = getResources().getResourceEntryName(newView.getId());
} catch (Resources.NotFoundException e) {
idName = String.valueOf(newView.getId());
}
Log.i(TAG, "Focused Id: " + idName + " Class: " + newView.getClass());
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
Thread(Runnable {
var oldId = -1
while (true) {
val newView: View? = this.currentFocus
if (newView != null && newView.id != oldId) {
oldId = newView.id
var idName: String = try {
resources.getResourceEntryName(newView.id)
} catch (e: Resources.NotFoundException) {
newView.id.toString()
}
Log.i(TAG, "Focused Id: " + idName + " Class: " + newView.javaClass)
}
try {
Thread.sleep(100)
} catch (e: InterruptedException) {
e.printStackTrace()
}
}
}).start()
このスレッドは100msサイクルで実行されるため、不要な作業でCPUがオーバーフローしないことに注意してください。