文章目录
方法之一:Broadcast receiver(广播接收者)
需求介绍:
点击fragment里的button,根据选择RadioButton的颜色进而改变activity的背景色。
演示:
代码演示:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="horizontal"
tools:context=".MainActivity">
<LinearLayout
android:id="@+id/ly"
android:background="@color/black"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
</LinearLayout>
<FrameLayout
android:id="@+id/fl"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2">
</FrameLayout>
</LinearLayout>
fragment_blank.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".BlankFragment">
<LinearLayout
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RadioGroup
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/rb1"
android:layout_width="24dp"
android:layout_height="24dp"
android:background="@drawable/circle"
android:button="@null"/>
<RadioButton
android:id="@+id/rb2"
android:layout_width="24dp"
android:layout_height="24dp"
android:background="@drawable/circle2"
android:button="@null"/>
</RadioGroup>
<Button
android:id="@+id/btn_set"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="set"/>
</LinearLayout>
</FrameLayout>
BlankFragment
public class BlankFragment extends Fragment{
private FragmentBlankBinding binding;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
binding = FragmentBlankBinding.inflate(getLayoutInflater());
binding.btnSet.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (binding.rb1.isChecked()) {
sendMsg(R.color.teal_200);
} else if (binding.rb2.isChecked()) {
sendMsg(R.color.purple_200);
}
}
});
return binding.getRoot();
}
/**
* 发送广播和数据
* @param color
*/
public void sendMsg(int color) {
Intent intent = new Intent();
intent.setAction("123");
intent.putExtra("200", color);
getContext().sendBroadcast(intent);
}
}
MainActivity
public class MainActivity extends AppCompatActivity {
private ActivityMainBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
addFragment(new BlankFragment());
IntentFilter filter = new IntentFilter();
filter.addAction("123");
registerReceiver(broadcastReceiver, filter);
}
private void addFragment(Fragment fragment) {
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.fl, fragment);
transaction.commit();
}
BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals("123")) {
int intExtra = intent.getIntExtra("200", 0);
runOnUiThread(new Runnable() {
@Override
public void run() {
binding.ly.setBackground(getDrawable(intExtra));
}
});
}
}
};
}