当用户单击菜单中的项目时,onOptionsItemSelected
会触发,并将 MenuItem
作为参数传递。这个传递的 MenuItem
扩展了 TextView
,与我们从 menu.findItem()
方法获得的不同。
要访问此用户需要单击该项目,我想在用户不单击菜单中的项目的情况下访问此对象。
最佳答案
在简历中只需调用 findViewById 并将其转换为 TextView。 您需要在计时器中进行此操作,以延迟代码,直到 View 变红,因为这是由菜单呈现的。 对我来说它是这样的
if (!created) { // for first resume only
created = true;
Timer t = new Timer();
t.schedule(new TimerTask() { // Might be done in better way, dont know yet
@Override
public void run() {
try {
while true {
final Object o = findViewById(R.id.mServerTime);
if (o == null) {
try {
Thread.sleep(1000);
} catch (Exception exp) {
}
} else {
if (o != null && o instanceof TextView) {
runOnUiThread(new Runnable() {
@Override
public void run() {
((TextView) o).setTextColor(Color.WHITE);
}
});
}
break;
}
}
}catch (Exception exp) {}
}
},1000);
关于android - 以编程方式访问 MenuItem 以更改 Textcolor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30205998/