以编程方式访问MenuItem以更改Textcolor

时间:2022-11-20 22:47:26

When User click on the item in the menu the onOptionsItemSelected fires passing MenuItem as an argument. this passed MenuItem extends the TextView and not the same that we can get from menu.findItem() method. To access this user needs to click on the item, I want to access this Object without user clicking on the item in the menu.

当用户单击菜单中的项目时,onOptionsItemSelected将传递MenuItem作为参数。这个传递的MenuItem扩展了TextView,而不是我们可以从menu.findItem()方法获得的。要访问此用户需要单击该项目,我想访问此对象,而无需用户单击菜单中的项目。

2 个解决方案

#1


in On resume just call findViewById and cast it to TextView. You need to make this in a Timer to postdelay your code until the view is rednered as this is rendered by menu. for me it works like this

在On resume中只需调用findViewById并将其强制转换为TextView。您需要在Timer中进行此操作以后延迟代码,直到视图被撤消,因为它是由菜单呈现的。对我来说它的工作原理就是这样

  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);

#2


I can't post comments in the answer above, so I'm writing another answer. A more reliable way to know when the view is actually placed on the screen is observing it's ViewTreeObserver such as:

我不能在上面的答案中发表评论,所以我正在写另一个答案。了解视图实际放置在屏幕上的更可靠方法是观察它的ViewTreeObserver,例如:

public class ActivityMain extends Activity {
    // Instance variables
    OnLayoutReadyListener onLayoutReadyListener = new OnLayoutReadyListener();
    View v;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        v = findViewById(R.id.mServerTime);
        v.getViewTreeObserver().addOnGlobalLayoutListener(onLayoutReadyListener);
    }

    class OnLayoutReadyListener implements ViewTreeObserver.OnGlobalLayoutListener {
        @Override
        public void onGlobalLayout() {
            // this will be called once view V is placed and measured on the screen
            v.setTextColor(Color.WHITE);
            // remove this listener to prevent any additional callbacks
            v.getViewTreeObserver().removeOnGlobalLayoutListener(onLayoutReadyListener);
        }
    }
}

#1


in On resume just call findViewById and cast it to TextView. You need to make this in a Timer to postdelay your code until the view is rednered as this is rendered by menu. for me it works like this

在On resume中只需调用findViewById并将其强制转换为TextView。您需要在Timer中进行此操作以后延迟代码,直到视图被撤消,因为它是由菜单呈现的。对我来说它的工作原理就是这样

  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);

#2


I can't post comments in the answer above, so I'm writing another answer. A more reliable way to know when the view is actually placed on the screen is observing it's ViewTreeObserver such as:

我不能在上面的答案中发表评论,所以我正在写另一个答案。了解视图实际放置在屏幕上的更可靠方法是观察它的ViewTreeObserver,例如:

public class ActivityMain extends Activity {
    // Instance variables
    OnLayoutReadyListener onLayoutReadyListener = new OnLayoutReadyListener();
    View v;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        v = findViewById(R.id.mServerTime);
        v.getViewTreeObserver().addOnGlobalLayoutListener(onLayoutReadyListener);
    }

    class OnLayoutReadyListener implements ViewTreeObserver.OnGlobalLayoutListener {
        @Override
        public void onGlobalLayout() {
            // this will be called once view V is placed and measured on the screen
            v.setTextColor(Color.WHITE);
            // remove this listener to prevent any additional callbacks
            v.getViewTreeObserver().removeOnGlobalLayoutListener(onLayoutReadyListener);
        }
    }
}