Android custom AlertDialog and Activity transmit data to each other
Main functions:
1. Obtain the string settings from the TextView of the Activity to the TextView and EditText of AlertDialog.
2. Set the value in the EditText of AlertDialog to the TextView of the Activity.
Effect:
Questions about the User-Defined AlertDialogTwo:
1. How to place custom layout in AlertDialog?
Answer:
After obtaining the view of layout, you can directly call the setView method of AlertDialog. Builder.
2. How to operate controls in custom AlertDialog?
Answer:
Similar to the operations in fragment, you must first obtain the view of the layout and then obtain the control from the view for operations.
MainActivity:
Package com. example. myalertdialog; import android. app. activity; import android. content. dialogInterface; import android. support. v7.app. alertDialog; import android. support. v7.app. appCompatActivity; import android. OS. bundle; import android. view. view; import android. widget. button; import android. widget. editText; import android. widget. linearLayout; import android. widget. textView; import android. widget. toast; public Class MainActivity extends Activity {TextView old_name; Button bt_change_name; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); old_name = (TextView) findViewById (R. id. TV _name); bt_change_name = (Button) findViewById (R. id. bt_name); bt_change_name.setOnClickListener (new View. onClickListener () {@ Override pub Lic void onClick (View v) {// obtain the view LinearLayout change_name = (LinearLayout) getLayoutInflater () of the custom AlertDialog layout file (). inflate (R. layout. my_dialog, null); TextView TV _name_dialog = (TextView) change_name.findViewById (R. id. TV _name_dialog); // because EditText must be operated on in the internal class, final EditText et_name_dialog = (EditText) change_name.findViewById (R. id. et_name_dialog); // set the TextView and EditText display Ac in AlertDialog Content of TextView in Tianyi TV _name_dialog.setText (old_name.getText (). toString (); et_name_dialog.setText (old_name.getText (). toString (); new AlertDialog. builder (MainActivity. this ). setTitle ("Modify user name "). setView (change_name ). setPositiveButton ("OK", new DialogInterface. onClickListener () {@ Override public void onClick (DialogInterface dialog, int which) {// display the textview in the Activity To the content in EditText in AlertDialog // and use Ast: old_name.setText (et_name_dialog.getText (). toString (); Toast. makeText (MainActivity. this, "set successfully! ", Toast. LENGTH_SHORT ). show () ;}) // because the "cancel" button is not set to the click effect, set it to null directly. setNegativeButton ("cancel", null ). create (). show ();}});}}
Activity_main.xml:
<! -- {Cke_protected} {C} % 3C! % 2D % 2D % 3 Fxml % 20 version % 3D % 221.0% 20 encoding % 3D % 22utf-8% 22% 3F % 2D % 2D % 3E --> <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 = "10dp"> <linearlayout android: layout_width = "match_parent" android: layout_height = "wrap_content" android: orientation = "horizontal"> <textview android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: textcolor = "#000" android: text = "original User name:"> <textview android: id = "@ + id/TV _name" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: text = "watermelon"> </textview> </linearlayout> <button android: id = "@ + id/bt_name" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: text = "Modify user name"> </button> </linearlayout>
My_dialog.xml:
<! -- {Cke_protected} {C} % 3C! % 2D % 2D % 3 Fxml % 20 version % 3D % 221.0% 20 encoding % 3D % 22utf-8% 22% 3F % 2D % 2D % 3E --> <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 = "10dp"> <linearlayout android: layout_width = "match_parent" android: layout_height = "wrap_content" android: orientation = "horizontal"> <textv Iew android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: textcolor = "#000" android: text = "original User name:"> <textview android: id = "@ + id/TV _name_dialog" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: text = "Hello World! "> </Textview> </linearlayout> <linearlayout android: layout_width =" match_parent "android: layout_height =" wrap_content "android: orientation = "horizontal"> <textview android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: textcolor = "#000" android: text = "new username"> <edittext android: id = "@ + id/et_name_dialog" android: layout_width = "match_parent" android: layout_height = "wrap_cont Ent "android: text =" Hello World! "> </Edittext> </textview> </linearlayout>