【发布时间】: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