更改MainActivity中按钮的背景



我创建了一个扩展DialogFragment的类。在该类中,当我按下一个按钮时,我想更改MainActivity中按钮的背景。

我试图创建一个内部类来访问MainActivity对象,但我得到的是这个错误:

尝试调用虚拟方法android.content.pm.ApplicationInfoandroid.content.Context.getApplicationInfo(('参考


@SuppressLint("ValidFragment")
public class Popup extends DialogFragment {
private final MediaPlayer _mediaPlayer = new MediaPlayer();
private final int _layout;
private TextInputEditText _customTextField;
private final Dialog mDialog = new Dialog();
public boolean dialogIsActive = false;
@SuppressLint("ValidFragment")
public Popup(int layout) {
_layout = layout;
}
public interface ICustomTts {
void customTts(String input, Activity activity);
}
public ICustomTts iCustomTts;
public interface  ITarget {
String getTarget(String input);
}
public ITarget iTarget;
@SuppressLint({"ClickableViewAccessibility", "ResourceType"})
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
final View view =  inflater.inflate(_layout, container, false);
// Display fragment_dialog
if (_layout == R.layout.fragment_dialog) {
// Read and display the expressions
view.findViewById(R.id.dialogHelp).setOnClickListener(v -> {
String expressions = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
expressions = Helper.readFile(getContext(), "expressions.yml");
}
TextView models = view.findViewById(R.id.dialogModels);
models.setText(expressions);
});
// Toggle the listener
Button cta = view.findViewById(R.id.dialogCta);
// Gain access to MainActivity
class InnerMainActivity extends my.app.MainActivity {
@RequiresApi(api = Build.VERSION_CODES.M)
public void dialog(boolean active) {
int button = (active) ? R.drawable.button_red : R.drawable.button_yellow;
findViewById(R.id.dialog).setBackground(ContextCompat.getDrawable(getContext(), button));
}
}
InnerMainActivity innerMainActivity = new InnerMainActivity();
cta.setOnClickListener(v -> {
if (!dialogIsActive) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (mDialog.hasRecordPermission(getContext())) {
mDialog.startService(getContext());
cta.setBackground(ContextCompat.getDrawable(getContext(), R.drawable.button_red));
cta.setText(R.string.dialog_do_not_listen);
cta.setTextSize(17);
innerMainActivity.dialog(true);
dialogIsActive = true;
}
}
}
else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
mDialog.stopTheService(getContext());
cta.setBackground(ContextCompat.getDrawable(getContext(), R.drawable.button_yellow));
cta.setText(R.string.dialog_listen);
innerMainActivity.dialog(false);
dialogIsActive = false;
}
}
});
}

return view;
}      
}

您可以使用接口来完成此操作。在对话框中片段:

公共类MyDialog扩展DialogFragment{

public boolean dialogIsActive = false;
public interface OnButtonClick {
void onButtonClick(boolean active);
}
private OnButtonClick onButtonClick;
public MyDialog(OnButtonClick onButtonClick) {
this.onButtonClick = onButtonClick;
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
final View view = inflater.inflate(_layout, container, false);
Button cta = view.findViewById(R.id.dialogCta);
cta.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//onButtonClick.onButtonClick(boolean)   this is where your activity trigger
if (!dialogIsActive) {
onButtonClick.onButtonClick(true);
dialogIsActive = true;
} else {
onButtonClick.onButtonClick(false);
dialogIsActive = false;
}
}
});
return view;
}

}

在您的活动中

public class MyActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyDialog myDialog= new MyDialog(new MyDialog.OnButtonClick(){
@Override
public void onButtonClick(boolean active) {
//this is where you know that the dialog button clicked
Button activityButton = findViewById(R.id.activityButton);
int button = (active) ? R.drawable.button_red : R.drawable.button_yellow;
activityButton.setBackground(ContextCompat.getDrawable(MyActivity.this, button));
}
});
myDialog.show(getSupportFragmentManager(),"tag");
}

}

最新更新