我正在尝试输入密码,当用户输入错误的密码时,会显示一个要求“取消”或“重试”的对话框,当用户点击“重试”时,它会再次显示密码提示。
以下图片说明了我的意思
我就是这样做的
/** RETRIEVE VIEW FROM DIALOGPROMPT.XML AND SET VIEW AS AN ALERTDIALOG BUILDER **/
LayoutInflater li = LayoutInflater.from(context);
View promptsView = li.inflate(R.layout.searchprompt, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
alertDialogBuilder.setView(promptsView);
final EditText userInput = (EditText) promptsView
.findViewById(R.id.user_input);
// set dialog message
alertDialogBuilder
.setCancelable(false)
.setNegativeButton("Go",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
/** DO THE METHOD HERE WHEN PROCEED IS CLICKED*/
String user_text = (userInput.getText()).toString();
/** CHECK FOR USER'S INPUT **/
if (user_text.equals("oeg"))
{
Log.d(user_text, "HELLO THIS IS THE MESSAGE CAUGHT :)");
Search_Tips(user_text);
}
else{
Log.d(user_text,"string is empty");
String message = "The password you have entered is incorrect." + " \n" + "Please try again";
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Error");
builder.setMessage(message);
builder.setPositiveButton("Cancel", null);
builder.setNegativeButton("Retry", null);
builder.create().show();
}
}
})
.setPositiveButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
});
有谁知道怎么做?
答案 0 :(得分:12)
我已经解决了自己的问题。我为我的警告对话框创建了一个方法,然后当我点击“重试”时,我将再次调用该方法。 :)
public void showDialog()
{
LayoutInflater li = LayoutInflater.from(context);
View promptsView = li.inflate(R.layout.searchprompt, null);
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
alertDialogBuilder.setView(promptsView);
final EditText userInput = (EditText) promptsView
.findViewById(R.id.user_input);
// set dialog message
alertDialogBuilder
.setCancelable(false)
.setNegativeButton("Go",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
/** DO THE METHOD HERE WHEN PROCEED IS CLICKED*/
String user_text = (userInput.getText()).toString();
/** CHECK FOR USER'S INPUT **/
if (user_text.equals("oeg"))
{
Log.d(user_text, "HELLO THIS IS THE MESSAGE CAUGHT :)");
Search_Tips(user_text);
}
else{
Log.d(user_text,"string is empty");
String message = "The password you have entered is incorrect." + " \n \n" + "Please try again!";
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Error");
builder.setMessage(message);
builder.setPositiveButton("Cancel", null);
builder.setNegativeButton("Retry", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
showDialog();
}
});
builder.create().show();
}
}
})
.setPositiveButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.dismiss();
}
}
);
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
答案 1 :(得分:4)
这是一个很好的样本 - 来自mkyong的http://www.mkyong.com/android/android-prompt-user-input-dialog-example。
密码提示的布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="10dp" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Type Your Message : "
android:labelFor="@+id/editTextDialogUserInput"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/editTextDialogUserInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword" >
<requestFocus />
</EditText>
</LinearLayout>
答案 2 :(得分:0)
尝试一下
public class LoginToApp extends DialogFragment {
public static final String TAG = "LoginDialog";
public static LoginToApp newInstance() {
LoginToApp login = new LoginToApp();
login.setStyle(STYLE_NO_TITLE, 0);
login.setCancelable(false);
return login;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.dialog_login_to_app, container, false);
final EditText passwordEditText = view.findViewById(R.id.dialog_login_password);
Button loginButton = view.findViewById(R.id.dialog_login_loginBtn);
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String password = passwordEditText.getText().toString();
if (getActivity() != null) {
CharSequence passwordFromShared;
passwordFromShared = "Your password";
assert passwordFromShared != null;
if (password.contains(passwordFromShared) && password.length()==passwordFromShared.length()) {
LoginToApp.super.dismiss();
} else {
//show some info when error
}
}
}
});
return view;
}
布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal|center"
android:layout_marginBottom="10dp"
android:background="@color/colorPrimary"
android:foregroundGravity="center_vertical|center_horizontal|center"
android:gravity="center_vertical|center_horizontal|center"
android:padding="10dp"
android:text="Login"
android:textColor="#000000"
android:textSize="12sp"
android:textStyle="bold" />
<EditText
android:id="@+id/dialog_login_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Password"
android:inputType="textPassword" />
<Button
android:id="@+id/dialog_login_loginBtn"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="@color/colorAccent"
android:fontFamily="sans-serif"
android:text="login"
android:textColor="#000000" />
用作:
LoginToApp loginToApp = LoginToApp.newInstance();
loginToApp.show(getSupportFragmentManager(),LoginToApp.TAG);