[英]Change background color of buttons from another activity onCheckedChange through shared preference(?)
我在MainActivity中有4个导致不同活动的按钮,默认情况下,它们被设置为具有图像背景。
我在主要活动中单击一个按钮,它将带我进入有开关的“选项”活动。 '
//creates instance of the button and redirects to appropriate activity/class on button click
//options
Button options = findViewById(R.id.options);
options.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intentOptions = new Intent(MainActivity.this, Options.class);
startActivity(intentOptions);
}
});
'
选中该开关后,我希望“选项”背景(供用户反馈)更改为一种颜色,因此MainActivity中有4个按钮(我不想启动MainActivity)。
但是,显然我收到了空对象引用错误,因为我尝试在另一个活动中更改smth。
我在这里阅读了很多类似的主题,并且我了解到我应该使用共享首选项存储smth(什么?),这将允许我更改另一个活动中按钮的背景,然后将其(如何?)传递给Options并将其传递返回(?)从开关上的选项中检查(???)。
我是Java和android的新手,我无法掌握如何解决问题。
选项类中的代码:
`
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.options);
//on toggle switch changes options background and activities buttons background in main layout to plain color
final Switch optionPlainColored = findViewById(R.id.switch1);
optionPlainColored.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
ImageView background = findViewById(R.id.background);
Button attractionsAndArchitecture = findViewById(R.id.attractions_and_architecture);
Button barsAndRestaurants = findViewById(R.id.bars_and_restaurants);
Button sportAndRecreations = findViewById(R.id.sports_and_recreation);
Button nightlifeAndCulture = findViewById(R.id.culture_and_nightlife);
//on switch toggle changes background of options activity and 4 buttons in main activity
if(optionPlainColored.isChecked()){
background.setImageResource(R.drawable.setoptionsbackgroundcolor2);
attractionsAndArchitecture.setBackgroundColor(getResources().getColor(R.color.attractionsPlainColor));
barsAndRestaurants.setBackgroundColor(getResources().getColor(R.color.barsPlainColor));
sportAndRecreations.setBackgroundColor(getResources().getColor(R.color.sportsPlainColor));
nightlifeAndCulture.setBackgroundColor(getResources().getColor(R.color.culturePlainColor));
}else{
//when unchecked reverses the change
background.setImageResource(R.drawable.optionsbackground);
attractionsAndArchitecture.setBackground(getResources().getDrawable(R.drawable.placestovisitbackground));
barsAndRestaurants.setBackground(getResources().getDrawable(R.drawable.barandrestuarantsbackground));
sportAndRecreations.setBackground(getResources().getDrawable(R.drawable.sportandrecreationbackground));
nightlifeAndCulture.setBackground(getResources().getDrawable(R.drawable.nightlifeandculturebackground));
}
}
});
//displays toast message about the switch
Toast.makeText(getApplicationContext(), "Toggling the switch will change background color of the Options and Homescreen to plain colored one",
Toast.LENGTH_LONG).show();
}
`
我正在更改背景的行上的空对象引用。
MainActivity上的XML:
'<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/backgroundinfoadditional"
tools:context=".MainActivity">
<Button
android:id="@+id/attractions_and_architecture"
style="@style/buttonToActivity"
android:layout_width="205dp"
android:layout_height="285dp"
android:layout_marginBottom="8dp"
android:background="@drawable/placestovisitbackground"
android:text="@string/attractions_and_architecture"
app:layout_constraintBottom_toTopOf="@+id/title"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/bars_and_restaurants"
style="@style/buttonToActivity"
android:layout_width="208dp"
android:layout_height="280dp"
android:layout_marginBottom="8dp"
android:background="@drawable/barandrestuarantsbackground"
android:text="@string/bars_and_restaurants"
app:layout_constraintBottom_toTopOf="@+id/title"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<Button
android:id="@+id/sports_and_recreation"
style="@style/buttonToActivity"
android:layout_width="208dp"
android:layout_height="284dp"
android:background="@drawable/sportandrecreationbackground"
android:text="@string/sports_and_recreation"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/title"
app:layout_constraintVertical_bias="0.0" />
<Button
android:id="@+id/culture_and_nightlife"
style="@style/buttonToActivity"
android:layout_width="204dp"
android:layout_height="282dp"
android:background="@drawable/nightlifeandculturebackground"
android:text="@string/culture_and_nightlife"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/title" />
<Button
android:id="@+id/options"
style="@style/options"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.024"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
<Button
android:id="@+id/info"
style="@style/info"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.952"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/title"
app:layout_constraintVertical_bias="1.0" />
<TextView
android:id="@+id/title"
style="@style/title"
android:layout_width="match_parent"
android:layout_height="39dp"
android:text="@string/moscow_concise_guide"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
'
使用处理程序将消息传递给mainactivity。 喜欢
if (MainActivity.xhandler != null) {
Message msg = new Message();
msg.what = message;
msg.obj = data1;
MainActivity.xhandler.sendMessage(msg);
}
在MainActivity中定义处理程序。
Handler xhandler=new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
return false;
}
});
得到了一个从其他来源获得的答案,将其发布在此处(不使用共享的偏好):“我认为使用单独的Activity作为选项的替代方法是使用DialogFragment。这将允许在其中显示浮动窗口在MainActivity布局的顶部,然后在其中加载选项布局,包括Switch。
按照这个想法,您需要为这些选项创建一个DialogFragment,而不是新的Activity。
然后,假设您在“活动”中有一个按钮,并且想要更改颜色。 您可以像这样参考按钮
getActivity()。findViewById(R.id.button)就是这样:slight_smile:
这样一来,您就可以访问“活动”中的任何视图,更改背景颜色,分配其他图像或任何所需的内容,而不必处理首选项或访问其他“活动”。
OptionsDialogFragment类:
import android.app.DialogFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.Switch;
public class OptionsDialogFragment extends DialogFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Here we inflate the layout to use with the options dialog
View dialogView = inflater.inflate(R.layout.options, container, false);
final Switch optionPlainColored = dialogView.findViewById(R.id.switch1);
optionPlainColored.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(optionPlainColored.isChecked()){
// we use getActivity() to refer to the views in the Activity layout.
getActivity().findViewById(R.id.attractions_and_architecture).setBackgroundColor(getResources().getColor(R.color.attractionsPlainColor));
getActivity().findViewById(R.id.bars_and_restaurants).setBackgroundColor(getResources().getColor(R.color.barsPlainColor));
getActivity().findViewById(R.id.sports_and_recreation).setBackgroundColor(getResources().getColor(R.color.sportsPlainColor));
getActivity().findViewById(R.id.culture_and_nightlife).setBackgroundColor(getResources().getColor(R.color.culturePlainColor));
} else {
getActivity().findViewById(R.id.attractions_and_architecture).setBackground(getResources().getDrawable(R.drawable.backgroundattractionsandarchitecture));
getActivity().findViewById(R.id.bars_and_restaurants).setBackground(getResources().getDrawable(R.drawable.backgroundbarsandrestaurants));
getActivity().findViewById(R.id.sports_and_recreation).setBackground(getResources().getDrawable(R.drawable.backgroundsportsandrecreation));
getActivity().findViewById(R.id.culture_and_nightlife).setBackground(getResources().getDrawable(R.drawable.backgroundnightlifeandculture));
}
}
});
return dialogView;
}
}'
在MainActivity中:
' Button options = findViewById(R.id.options);
options.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
OptionsDialogFragment dialogFrag = new OptionsDialogFragment();
FragmentManager fm = getFragmentManager();
dialogFrag.show(fm, "Options");
}
});'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.