When we use Dialog, if we need to use a lot of self-designed controls, although the pop-up box can display the interface we need, we cannot find a place to complete the writing of the control code. How to solve this problem, we can disguise the Activity It becomes a Dialog pop-up box, which shows the interface. Writing control code in Activity is also everyone's forte. Now I'll talk about a simple implementation.
First of all, the key to the problem is the sentence android:theme="@android:style/Theme.Dialog" in MainActivity, which is the Dialog style of Activity.
We first create a main.xml with the following content
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id = " @+id/showString " android:layout_width = " fill_parent " android:layout_height = " wrap_content " android:text = " Show the numbers entered in the dialog here: " /> <Button android:id="@+id/openButton" android:text="点此打开Dialog" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
Create another textdialog.xml with the following content
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <EditText android:id="@+id/et" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/returnButton" android:text="请输入字符" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
Now write the following code in MainActivity, which is very basic code, I believe everyone can understand public class MainActivity extends Activity { private Button openButton; private TextView showString; public void onCreate(Bundle savedInstanceState) { super.onCreate (savedInstanceState); setContentView(R.layout.main); openButton = (Button)findViewById(R.id.openButton); showString = (TextView)findViewById(R.id.showString); openButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { // The basic usage of returning to test Activity is used here. Because it has nothing to do with the theme, I will not explain more Intent i = new Intent(MainActivity. this , testDialog. class ); startActivityForResult (i, 0 ); } }); } // Use the return test Activity to receive the input data and display it, which proves that our Dialog-style Activity can indeed complete the data processing protected void onActivityResult( int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); // Take out the string Bundle bundle = data.getExtras(); String str = bundle.getString("str"); showString.setText(str); } }
The following is the programming of the testDialog, you can see that this Dialog is no different from a normal Activity, but it can indeed pop up like a Dialog in the end
ublic class testDialog extends Activity{ private Button returnButton; private EditText inputEditor; protected void onCreate(Bundle savedInstanceState) { super.onCreate (savedInstanceState); setContentView(R.layout.textdialog); returnButton = (Button)findViewById(R.id.returnButton); inputEditor = (EditText)findViewById(R.id.et); // Same as before, just use the basic method of returning Activity, although it is already a Dialog, it is no different from ordinary Activity returnButton.setOnClickListener( new OnClickListener () { public void onClick(View v) { String input = inputEditor.getText().toString(); Intent i = new Intent(testDialog.this, MainActivity.class); Bundle b = new Bundle(); b.putString("str", input); i.putExtras(b); testDialog.this.setResult(RESULT_OK, i); testDialog.this.finish(); } }); } }
The last highlight is to set the Dialog style of the Activity, and register the second Activity in the MainActivity. Don't finish the style setting.
<activity android:name= " .testDialog " android:label = " This is an Activity that becomes a Dialog " android:theme = " @android:style/Theme.Dialog " ></activity>
Ok, you can run it, if it is normal, you will see the same result as me