简体   繁体   中英

Android Alert Dialog on startup

How can I achieve to show a Dialog automatically when an activity starts. If the activity is started a Dialog must be shown, where you must type a password. This password will be checked with the password stored in sharedpreferences, and if it is correct this activity will be shown,if not, a message will be shown on the dialog that the password is wrong and he must type it again.. I looked for some tutorials but all of them used a button to start the AlertDialog, but in my case It mus be shown when a specific activity is called.

How can I achieve it?

Add this in your manifest, in the activity you want to look like dialog, declaration:

<activity android:theme="@android:style/Theme.Dialog">

for more information and themes: http://developer.android.com/guide/topics/ui/themes.html

furthermore, to this proggramatically you can use the following code:

public class ShowDialogActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    //
    //Log.d("DEBUG", "showing dialog!");

    Dialog dialog = new Dialog(this);
    dialog.setContentView(R.layout.select_dialog_singlechoice);
    dialog.setTitle("Your Widget Name");
    dialog.setCancelable(true);
    dialog.setCanceledOnTouchOutside(true);
    TextView text = (TextView) dialog.findViewById(R.id.text1);
    text.setText("Message");

    dialog.show();
    //
   dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {

    public void onCancel(DialogInterface arg0) {
        finish();
    }

   });
}

}

You can choose whatever layout you wish for the dialog and design it as you want.

In addition you would need to set this activity declaration in the manifest for the following:

<activity android:name=".ShowDialogActivity"
          android:theme="@android:style/Theme.Translucent.NoTitleBar">
</activity>

Hope this is what you was looking for.

in your oncreate method add this alert dialog code,and validate the input from edittext

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layoutname);

    AlertDialog.Builder alert = new AlertDialog.Builder(this);

    alert.setTitle("Title");
    alert.setMessage("Message");

    // Set an EditText view to get user input 
    final EditText input = new EditText(this);
    alert.setView(input);

    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
      String value = input.getText();
      // Do something with value!
      }
    });

    alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int whichButton) {
        // Canceled.
      }
    });

    alert.show();
}

Another solution is: do not show dialog,create an activity which looks like dialog,create activity and give it a dialog look:

<activity
    android:label="@string/app_name"
    android:name=".DialogActivityDemoActivity"
    android:theme="@android:style/Theme.Dialog" >
    </activity>

now make this activity a launcher activity,validate user's input then start your main activity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM