要让DialogFragment在Activity的主界面中更改文本,可以按照以下步骤进行操作:
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="初始文本"
/>
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
}
public void showDialogFragment() {
MyDialogFragment dialogFragment = new MyDialogFragment();
dialogFragment.show(getSupportFragmentManager(), "dialog");
}
public interface OnTextChangeListener {
void onTextChange(String newText);
}
OnTextChangeListener listener = (OnTextChangeListener) getActivity();
listener.onTextChange("新的文本");
public class MainActivity extends AppCompatActivity implements OnTextChangeListener {
// ...
@Override
public void onTextChange(String newText) {
textView.setText(newText);
}
}
通过以上步骤,就可以实现在DialogFragment中更改Activity主界面中的文本内容。
领取专属 10元无门槛券
手把手带您无忧上云