我在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(什么?),这将允许我更改另一个活动中按钮的背景,然后将其传递给(如何?)到选项并从Switch check(???)上的选项中传回(?)。
我是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>
&#39;
答案 0 :(得分:0)
使用handler将消息传递给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;
}
});
答案 1 :(得分:0)
得到了其他来源的答案,在此发布(这不使用共享偏好): &#34;我认为使用单独的Activity作为选项的替代方法是使用DialogFragment。这将允许在MainActivity布局的顶部显示一个浮动窗口,并在那里加载选项布局,包括Switch。
遵循这个想法,您需要为选项创建DialogFragment而不是新的Activity。
然后,假设您的“活动”中有一个按钮,并且您想要更改颜色。你可以参考像这样的按钮
getActivity()。findViewById(R.id.button) 就是这样:slight_smile:
通过这种方式,您可以访问“活动”中的任何视图,更改背景颜色,指定其他图像或任何您想要的视图,而无需处理首选项或访问其他活动。&#34;
OptionsDialogFragment类: &#39;
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");
}
});'