Reputation: 15
I'm experimenting making a selection option via dialog fragment. The options are, manual input, and preset input, the preset being represented by two buttons. The current issue i'm facing, is that when a press the button, i want the dialog to close and set the information to the target TextView, i've been unable to find a solution.
Currently with buttonfps2397 i'm populating the EditText within the dialog and pressing ok to send it to the target TextView, one click to much for me.
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);
}
Upvotes: 1
Views: 927
Reputation: 54194
Instances of DialogFragment
have a dismiss()
method. You can call this from your button click listeners in order to close the dialog.
buttonfps2397.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String setFps2397 = "23,97";
editTextFpsEntry.setText(setFps2397);
dismiss(); // <---- closes the dialog
}
});
Upvotes: 2