如何在活动开始时自动显示对话框。 如果活动已启动,则必须显示对话框,您必须输入密码。 该密码将与存储在sharedpreferences中的密码一起检查,如果它是 ,则会显示此活动,否则将显示对话框中显示密码错误的消息,并且他必须再次输入。寻找一些教程,但他们都使用一个按钮来启动AlertDialog,但在我的情况下,它会显示当一个特定的活动被调用。启动时的Android警报对话框
我该如何实现它?
如何在活动开始时自动显示对话框。 如果活动已启动,则必须显示对话框,您必须输入密码。 该密码将与存储在sharedpreferences中的密码一起检查,如果它是 ,则会显示此活动,否则将显示对话框中显示密码错误的消息,并且他必须再次输入。寻找一些教程,但他们都使用一个按钮来启动AlertDialog,但在我的情况下,它会显示当一个特定的活动被调用。启动时的Android警报对话框
我该如何实现它?
在您的清单中添加这一点,在活动中,你想要的样子对话框,声明:
<activity android:theme="@android:style/Theme.Dialog">
以获取更多信息和主题:http://developer.android.com/guide/topics/ui/themes.html
此外,本proggramatically您可以使用下面的代码:
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>
希望这是你一直在寻找的东西。
+1你可能已经实现了这只是一个'AlertDialog',但你的解决方案仍然完成工作 – John
谢谢,所以如果我当我的主要活动被调用时想要显示这个活动或活动对话框,我只需在我的MainActivity的onCreate-Method中使用一个Intent来启动这个活动吧?因为我的应用程序的主要目标是如果主要活动被称为对话框必须显示一个EditText字段,所以用户可以给一个密码...所以我只想显示我的主要活动只有当密码正确 – androidBeginner
第一开始这项活动......如果用户成功登录,然后开始您的主要活动。我想没有理由首先开始你的主要活动。 –
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();
}
Thx这也是我的问题的另一个很好的解决方案。我怎么能设法再次显示相同的对话框如果例如取消按钮被点击(在我的情况下,如果密码是错误的 - 但没关系,只想知道我怎么能在做某事后再次显示相同的对话框)只想在密码正确后显示活动 – androidBeginner
在方法中提取此警报对话框代码,并根据需要多次运行它! @androidBeginner – Hamad
另一种解决方案是添加此警告对话框代码,验证输入: 不显示对话框,创建它看起来像对话的活动,创建活动,并给它一个对话框:
<activity
android:label="@string/app_name"
android:name=".DialogActivityDemoActivity"
android:theme="@android:style/Theme.Dialog" >
</activity>
现在使此活动成为启动器活动,验证用户的输入,然后开始您的主要活动。
您可以使用其他活动。并将此活动的布局设置为对话框。 –
如何将活动的布局设置为对话框? – androidBeginner
相关:http://stackoverflow.com/questions/3011361/alertdialog-input-text – John