【问题标题】:How to properly access and change other views如何正确访问和更改其他视图
【发布时间】:2015-01-21 20:58:03
【问题描述】:

我正在尝试构建一个功能,如果一个页面被长按,配色方案将在应用程序的每个页面上变为黑白。如果页面或其他页面被长按,更改应该会恢复。这是我所拥有的样本。如果您想查看更具体的代码片段,请告诉我。

View sigView;
View rootView;
View tunerView;
public boolean pm;
public SharedPreferences sharedPreferences;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    sigView = inflater.inflate(R.layout.time_sig, container,false);
    rootView=inflater.inflate(R.layout.activity_metronome,container);
    tunerView=inflater.inflate(R.layout.activity_tuner,container);

    final TextView tvTempo=(TextView) rootView.findViewById(R.id.metro_disp);
    final TextView NumView = (TextView) sigView.findViewById(R.id.Sig_Num);
    final TextView tv_note=(TextView) tunerView.findViewById(R.id.tv_note);        
    final ImageView time_sig_bar= ImageView)sigView.findViewById(R.id.time_sig_bar);

    //**Lots more lines just like above for other elements

time_sig_bar.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View view) {
            if (!pm) {
                rootView.setBackgroundColor(Color.BLACK);
                //**Code to run

                pm=true;
                sharedPreferences.edit().putBoolean("pm",pm).apply();
            } else{
                rootView.setBackgroundColor(Color.TRANSPARENT);
            //**More code to run

                pm=false;
                sharedPreferences.edit().putBoolean("pm",pm).apply();
            }
//**code is copied for other elements to respond to longclick

我将此代码复制并粘贴到其他页面(进行了一些小的更改)。在其他页面之一上,该页面上的元素会更改,但不会更改其他页面上的元素。另外,我不知道如何设置它,以便颜色会从另一页变回来。任何想法我做错了什么?感谢您的帮助。

【问题讨论】:

    标签: java android view wear-os onlongclicklistener


    【解决方案1】:

    也许有一个新类来保存您的视图的状态数据。如果长按,则为真,否则为假。然后你可以将 longlick 上的状态设置为当前不是的状态

    【讨论】:

      猜你喜欢
      • 2011-12-24
      • 2019-08-19
      • 2015-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-23
      • 1970-01-01
      • 2014-02-04
      相关资源
      最近更新 更多