我正在尝试通过对话框片段进行选择。选项包括手动输入和预设输入,预设由两个按钮表示。我面临的当前问题是,当按下按钮时,我希望对话框关闭并将信息设置为目标TextView,但我一直无法找到解决方案。
当前,使用buttonfps2397,我在对话框中填充EditText,然后按OK将其发送到目标TextView,这对我来说是一击。
public class FpsDialog extends AppCompatDialogFragment{
private EditText editTextFpsEntry;
private FpsDialogListener listener;
private Button buttonfps2397;
private Button buttonfps50;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.fps_dialog, null);
builder.setView(view)
.setTitle("Login")
.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
})
.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
String fpsinfo = editTextFpsEntry.getText().toString();
listener.applyText(fpsinfo);
}
});
editTextFpsEntry = view.findViewById(R.id.fpsEntry);
buttonfps2397 = view.findViewById(R.id.fps2397);
buttonfps50 = view.findViewById(R.id.fps50);
buttonfps2397.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String setFps2397 = "23,97";
editTextFpsEntry.setText(setFps2397);
}
});
buttonfps50.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String setFps50 = "50";
listener.applyText(setFps50);
}
});
return builder.create();
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
try {
listener = (FpsDialogListener) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString()+
"must implement Lens Dialog Listener");
}
}
public interface FpsDialogListener{
void applyText(String fpsinfo);
}
答案 0 :(得分:1)
<article>
<h1>This is a pink page</h1>
<section id="home">
Pink pink dog cat cow chicken boy
</section>
<section id="about">
this is a story about a pink dog named Clyde.
</section>
<section id="projects">
And more on the Clyde story. He has a boy.
</section>
</article>
<button id="pink-btn">'pink'->'blue'</button> | <button id="pink-boy-btn">'pink'->'blue', 'boy'->'girl'</button> | <button id="pink-boy-dog-btn">'pink'->'blue', 'boy'->'girl', 'dog'->'cat'</button>
的实例具有DialogFragment
方法。您可以从按钮单击侦听器中调用此命令,以关闭对话框。
dismiss()