drawable/shape_dialog_edit.xml
1 2 3 4 5 6 7 8 9 10 11 12 |
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <stroke android:width="1dp" android:color="#999" /> <padding android:bottom="5dp" android:left="5dp" android:right="5dp" android:top="5dp" /> </shape> |
layout/fragment_input.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="20dp"> <TextView android:id="@+id/dialog_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="5dp" android:textSize="18sp" /> <EditText android:id="@+id/dialog_edit" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:background="@drawable/shape_dialog_edit" /> </LinearLayout> |
InputFragment.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
package com.phaisarn.myapplication; import android.os.Bundle; import android.view.View; import android.app.Dialog; import android.content.DialogInterface; import android.support.v4.app.DialogFragment; import android.support.v7.app.AlertDialog; import android.text.InputType; import android.widget.EditText; import android.widget.TextView; public class InputFragment extends DialogFragment { public InputFragment() { // Required empty public constructor } enum DataType { String, Number, Password } public static InputFragment newInstance(String message, DataType type) { InputFragment dialog = new InputFragment(); Bundle args = new Bundle(); args.putString("message", message); args.putSerializable("type", type); dialog.setArguments(args); return dialog; } @Override public Dialog onCreateDialog(Bundle savedInstanceState) { String message = getArguments().getString("message"); View view = getActivity().getLayoutInflater().inflate(R.layout.fragment_input, null); TextView textView = view.findViewById(R.id.dialog_text); textView.setText(message); final EditText editText = view.findViewById(R.id.dialog_edit); DataType type = (DataType)getArguments().getSerializable("type"); if(type == DataType.String) { editText.setInputType(InputType.TYPE_CLASS_TEXT); } else if(type == DataType.Number) { editText.setInputType(InputType.TYPE_CLASS_NUMBER); } else if(type == DataType.Password) { editText.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_CLASS_TEXT); } AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity()) .setView(view) .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }) .setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { mListener.onFinishDialog(editText.getText().toString()); } }); return alertDialogBuilder.create(); } interface OnFinishDialogListener { void onFinishDialog(String result); } private OnFinishDialogListener mListener; public void setOnFinishDialogListener(OnFinishDialogListener listener) { mListener = listener; } } |
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="31dp" android:text="Show Dialog" /> </LinearLayout> |
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
package com.phaisarn.myapplication; import android.support.v4.app.FragmentManager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button = findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { showInputDialog(); } }); } private void showInputDialog() { FragmentManager fm = getSupportFragmentManager(); InputFragment dialog = InputFragment.newInstance("Please enter data:", InputFragment.DataType.String); dialog.show(fm, null); dialog.setOnFinishDialogListener(new InputFragment.OnFinishDialogListener() { @Override public void onFinishDialog(String result) { Toast.makeText(getBaseContext(), result, Toast.LENGTH_SHORT).show(); } }); } } |