If you use showDialog(int), the activity will call onCreateDialog() method the first time, and hang onto it thereafter. Any dialog that is created by this method will automatically be saved and restored for you, including whether it is showing.
If you would like an opportunity to prepare your dialog before it is shown, override onPrepareDialog(int, Dialog, Bundle). We will pass the text from main activity to our custom dialog, so we have to implement onPrepareDialog(int, Dialog, Bundle). And the text will be passed to dislog in bundle.
main activity
package com.AndroidCustomDialog; import android.app.Activity; import android.app.Dialog; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class AndroidCustomDialogActivity extends Activity { EditText editTextPass; Button buttonOpenDialog; String KEY_TEXTPSS = "TEXTPSS"; static final int CUSTOM_DIALOG_ID = 0; TextView dialog_TextView; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); editTextPass = (EditText)findViewById(R.id.textpass); buttonOpenDialog = (Button)findViewById(R.id.opendialog); buttonOpenDialog.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View arg0) { // TODO Auto-generated method stub Bundle bundle = new Bundle(); bundle.putString(KEY_TEXTPSS, editTextPass.getText().toString()); showDialog(CUSTOM_DIALOG_ID, bundle); }}); } @Override protected Dialog onCreateDialog(int id) { Dialog dialog = null; switch(id) { case CUSTOM_DIALOG_ID: dialog = new Dialog(AndroidCustomDialogActivity.this); dialog.setContentView(R.layout.dialoglayout); dialog.setTitle("Custom Dialog"); dialog_TextView = (TextView)dialog.findViewById(R.id.dialogtext); Button dialog_OK = (Button)dialog.findViewById(R.id.dialog_ok); dialog_OK.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub Toast.makeText(AndroidCustomDialogActivity.this, "Dismiss by OK button", Toast.LENGTH_LONG).show(); dismissDialog(CUSTOM_DIALOG_ID); }}); Button dialog_Cancel = (Button)dialog.findViewById(R.id.dialog_cancel); dialog_Cancel.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub Toast.makeText(AndroidCustomDialogActivity.this, "Dismiss by Cancel button", Toast.LENGTH_LONG).show(); dismissDialog(CUSTOM_DIALOG_ID); }}); break; } return dialog; } @Override protected void onPrepareDialog(int id, Dialog dialog, Bundle bundle) { // TODO Auto-generated method stub super.onPrepareDialog(id, dialog, bundle); switch(id) { case CUSTOM_DIALOG_ID: dialog_TextView.setText("Text passed to Dialog: " + bundle.getString(KEY_TEXTPSS)); break; } } }
main layout
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <EditText android:id="@+id/textpass" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/opendialog" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Open Dialog" /> </LinearLayout>
/res/layout/dialoglayout.xml, it's the layout of the custom dialog.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/customdialog" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="20dp"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher"/> <TextView android:id="@+id/dialogtext" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <Button android:id="@+id/dialog_ok" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="OK" /> <Button android:id="@+id/dialog_cancel" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="cancel" /> </LinearLayout>
Next:
- Cancel and Dismiss Custom dialog
0 Response to "Custom dialog, with data passed in Bundle"
Posting Komentar