繁体   English   中英

启动时的Android警报对话框

[英]Android Alert Dialog on startup

如何在活动开始时自动显示Dialog。 如果活动已启动,则必须显示“对话框”,您必须在其中键入密码。 将使用存储在sharedpreferences中的密码检查此密码,如果正确,将显示此活动,否则,对话框中将显示密码错误的消息,并且必须再次键入...我查找一些教程,但他们都使用一个按钮来启动AlertDialog,但在我的情况下,它会在调用特定活动时显示。

我怎样才能实现它?

将此添加到您的清单中,在您想要看起来像对话框的活动中,声明:

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

有关更多信息和主题: http//developer.android.com/guide/topics/ui/themes.html

此外,通过这个程序,您可以使用以下代码:

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();
    }

   });
}

}

您可以选择对话框所需的任何布局,并根据需要进行设计。

此外,您需要在清单中为以下内容设置此活动声明:

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

希望这是你在寻找的东西。

在您的oncreate方法中添加此警报对话框代码,并验证来自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();
}

另一个解决方案是:不显示对话框,创建一个看起来像对话框的活动,创建活动并给它一个对话框看起来:

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

现在将此活动作为启动器活动,验证用户的输入然后启动您的主要活动。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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