Android Development notes (11) -- DialogFragment & click to listen, android development fragment
Reprinted Please note: http://www.cnblogs.com/igoslly/p/6931519.html
DialogFragment Usage & click to listen
/* DialogFragment is a pop-up box for displaying an interface on the Activity, such as an input box, a warning box, and a confirmation box.
* DialogFragment separately sets the layout XML file
* To use DialogFragment, at least the onCreateView or onCreateDIalog method must be implemented;
* OnCreateView: displays the Dialog using the defined xml layout file.
* OnCreateDialog: You can use AlertDialog or Dialog to create a Dialog.
*/
Below are some examples I have written myself:
1. Modify the name input box
Layout file:
1. Enter the EditBox control to enter the new user name.
2. Confirm the ImageView and click monitor to obtain the input characters.
3. The exit Button is not modified.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <EditText android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:hint="Please enter new user name ..." android:inputType="text" android:id="@+id/opponent_name_edit"/> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/ic_edit" android:id="@+id/opponent_name_confirm"/> </LinearLayout> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="BACK" android:textSize="24sp" android:layout_marginTop="10dp" android:id="@+id/edit_dialog_back"/></LinearLayout>
2. Click box for Content Selection
/* DialogFragment activity settings are the same as general Fragment settings.
* I generally use the onCreateView method.
*/
public class EditDialogue extends DialogFragment { private EditText mNewNameEditText; private ImageView mNewNameConfirm; private String player; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){ View rootView =inflater.inflate(R.layout.opponent_name_edit,container); mNewNameEditText = (EditText) rootView.findViewById(R.id.opponent_name_edit); mNewNameConfirm = (ImageView) rootView.findViewById(R.id.opponent_name_confirm); Button backButton = (Button) rootView.findViewById(R.id.edit_dialog_back); backButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { player=""; dismiss(); } });
mNewNameConfirm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
player = mNewNameEditText.getText().toString();
setData();dismiss();
}});
return rootView; }}
/* Click Detection
* You can set the Onclick attribute in the XML file by clicking a button to directly call the MainActivity method.
*/
anroid:OnClick="RecordName"
// Set the function in Activity
Public void RecordName (View view) {EditBox editbox = findViewById (R. id. name_edit); String newName = editbox. getText (). toString (); // here getText () is obtained as Editable type, toString () Conversion}
/* Click Detection
* In Fragment, OnclickLisener is used for listening.
* OnclickLisener is a function of the View class. Therefore, it can be used for buttons and controls inherited from the View class.
* The input box also listens to the ImageView and sends data to close the Dialog
*/
mNewNameConfirm.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { player = mNewNameEditText.getText().toString(); setData();dismiss(); }});
The content of this note is for personal study. For more information, see blog-igoslly.