如何将按钮连接到对话框,该对话框将使用用户输入在FrameLayout中编辑TextView? [英] How do I connect a button to a Dialog that will edit a TextView, within a FrameLayout, with a users input?
问题描述
我根本不了解DialogFragment.如何创建一个,如何从中获得用户输入,然后将其设置为TextView.
I don't understand DialogFragment at all. How to create one, how to get the user input out of it, and set it into a TextView.
单击TITLE按钮时,我想弹出一个DialogFragment,要求用户输入情绪板的标题.用户输入标题.当他们单击PostiveButton的完成"时,用户的标题设置在情绪面板的左上方,该面板具有带提示的TextView.
I would like for the TITLE button, when clicked, to bring up a DialogFragment asking the user to enter the title of their Mood Board. The user enters a title. When they click the PostiveButton "Done", the user's title is set into the top left frame of the mood board, which has a TextView with a hint.
请!提出问题,因为我不太了解对话框的设置.
Please! Ask questions, because I don't really understand the dialog setup.
这是我的MainActivity中的main_layout的图片.每个元素都有一个"@ + id/".
Here is a picture of my main_layout, in my MainActivity. Every element has an "@+id/".
推荐答案
您正在寻找的解决方案是回调:
The solution you are looking for is a callback:
- 使用用作回调的方法创建接口
- 在活动上实现界面
- 创建对话框片段,然后在onAttach中获取界面
- 在活动中显示对话框片段
- 关闭对话框片段时,使用接口实例传递文本
interface Callback {
updateText(String text)
}
class CoolActivity... implements Callback
onCreate {
//find your views
showDialogBtn.setOnClickListener(...
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
Fragment prev = getSupportFragmentManager().findFragmentByTag("yourTag");
if (prev != null) {
ft.remove(prev);
}
ft.addToBackStack(null);
DialogFragment dialogFragment = ExampleDialogFragment.newInstance();
dialogFragment.show(ft, "yourTag");
)
}
@Override
updateText(String text) {
youtView.setText(text)
}
class CoolDialogFragment extend DialogFragment {
private Callback callback;
@Override
void onAttach(Context context) {
callback = (Callback) context
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = new Dialog(getActivity());
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
return dialog;
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.dialog_fragment_example, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//find the views in the dialog fragment
closeBtn.clickListener(...
callback.updateText(edittext.getText().toString())
dismiss()
)
}
}
这是对话框片段的要点 https://gist.github.com/cutiko/7e307efcf7492ea52ef05cd90f9e3203
Here is a gist of a dialog fragment https://gist.github.com/cutiko/7e307efcf7492ea52ef05cd90f9e3203
问题是您想将对话框片段与另一个组件连接起来,并且希望直接进行.这不是一个好习惯,因为yiu会创建2个高度连接的组件,因此最好是使用数据持久性和某种形式的React编程
The problem is you want to connect a dialog fragment with a another component, and you want to do it straigth forward. This is not considered a good practice because yiu create 2 componentes higly attached, so the best would be to use data persistence and some form of react programming
这篇关于如何将按钮连接到对话框,该对话框将使用用户输入在FrameLayout中编辑TextView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!