2013-03-06 35 views
5

Tôi tương đối mới đối với Android/Java. Nhờ Stack Overflow, tôi đã có thể học được rất nhiều từ các câu hỏi được hỏi ở đây. Tuy nhiên, bây giờ tôi đang bị mắc kẹt về vấn đề này.Hiển thị nhiều AlertDialogs

Tôi có đầu vào mật khẩu AlertDialog bật lên khi chúng tôi khởi động ứng dụng. Nó đọc mật khẩu từ EditText và so sánh mật khẩu này với mật khẩu được lưu trữ trong một tệp. Tôi cần thêm AlertDialog hiển thị khi mật khẩu sai/không hợp lệ được cung cấp. Điều này tôi cũng thực hiện.

Bây giờ, hộp thoại thứ hai này có hai nút - Đặt lạiThử lại. Tôi muốn hoạt động hiển thị lại hộp thoại đầu tiên khi chúng tôi nhấp vào Thử lại. Đây là nơi tôi không biết gì. Nếu bất cứ ai có thể cung cấp một giải pháp làm việc và một chút giải thích, tôi sẽ rất có nghĩa vụ.

Đây là mã của tôi:

LayoutInflater li = LayoutInflater.from(context); 
View passView = li.inflate(R.layout.authdialog, null); 

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context); 

// set prompts.xml to alertdialog builder 
alertDialogBuilder.setView(passView); 

final EditText passInput = (EditText) passView.findViewById(
     R.id.editTextDialogUserInput); 
final TextView txtv = (TextView) findViewById(R.id.textv); 

// set dialog message 
alertDialogBuilder 
     .setCancelable(false) 
     .setPositiveButton("OK", new DialogInterface.OnClickListener() { 

      public void onClick(DialogInterface dialog, int id) { 
       String data = ""; 
       try { 
        FileInputStream fis = openFileInput("authfile"); 

        InputStreamReader in = new InputStreamReader(fis); 
        BufferedReader br = new BufferedReader(in); 
        data = br.readLine(); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 

       if (data.toString().equals(
         passInput.getText().toString())) { 
        txtv.setText("You Have Logged in"); 
       } else { 
        LayoutInflater ln = LayoutInflater.from(context); 
        View invalidView = ln.inflate(R.layout.invdialog, 
          null); 
        AlertDialog.Builder invalidDialogBuild = new AlertDialog.Builder(
          context); 
        invalidDialogBuild.setView(invalidView); 

        // set dialog message 
        invalidDialogBuild 
          .setCancelable(false) 
          .setPositiveButton(
            "Retry", 
            new DialogInterface.OnClickListener() { 

             public void onClick(
               DialogInterface dialog, 
               int id) { 
              dialog.cancel(); 
             } 
            }) 
          .setNegativeButton(
            "Reset", 
            new DialogInterface.OnClickListener() { 

             public void onClick(
               DialogInterface dialog, 
               int id) { 
              dialog.cancel(); 
             } 
            }); 

        dialog.cancel(); 

        AlertDialog invalidDialog = invalidDialogBuild 
          .create(); 

        // show it 
        invalidDialog.show(); 
       } 
      } 
     }) 
     .setNegativeButton("Cancel", 
       new DialogInterface.OnClickListener() { 

        public void onClick(DialogInterface dialog, int id) { 
         dialog.cancel(); 
        } 
       }); 

// create alert dialog 
AlertDialog alertDialog = alertDialogBuilder.create(); 

// show it 
alertDialog.show(); 
+1

Tôi biết bạn đã có các hộp thoại hoạt động một phần. Nhưng tôi thực sự tìm thấy nó một trải nghiệm người dùng tốt hơn để không sử dụng hộp thoại cho mục đích đó. Thay vào đó tôi có thể có màn hình đăng nhập của tôi 'Hoạt động' với' EditView' cho tên người dùng và mật khẩu, nút Gửi vv. Và tôi cũng có một tin nhắn 'TextView' mà tôi cập nhật sang màu đỏ và một tin nhắn (sử dụng' theTextView.setText ("" Bằng cách này bạn không bật lên bất cứ điều gì chỉ đơn giản là thay đổi một tin nhắn văn bản để cảnh báo người dùng tên người dùng của họ là không hợp lệ – wired00

+0

Thực tế đây là một ứng dụng duy nhất một người, nơi họ không cần tên người dùng mà chỉ cần mật khẩu Và tôi cần điều đó được thực hiện trên màn hình splash thay vì sau khi hiển thị màn hình trong 2 giây hoặc ít hơn mật khẩu phải được hỏi. Bạn cũng có thể đạt được cách tiếp cận thông qua hộp thoại bằng cách thay đổi textview ở trên đầu thay vì poping một thông điệp mới, nhưng sau đó sẽ không có tùy chọn đặt lại.Tôi có thể có ba nút trong một hộp thoại cảnh báo – GunJack

Trả lời

1

Tạo hai phương pháp riêng biệt của các hộp thoại của bạn và gọi cho họ như tôi đã làm trong tầm kiểm soát mã bên dưới ra.

private void dialogShow() 
{ 
    LayoutInflater li = LayoutInflater.from(context); 
    View passView = li.inflate(R.layout.authdialog, null); 
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context); 
    // set prompts.xml to alertdialog builder 
    alertDialogBuilder.setView(passView); 
    final EditText passInput = (EditText) passView.findViewById(R.id.editTextDialogUserInput); 
    final TextView txtv = (TextView) findViewById(R.id.textv); 
    // set dialog message 
    alertDialogBuilder.setCancelable(false) 
      .setPositiveButton("OK", new DialogInterface.OnClickListener() 
      { 
      public void onClick(DialogInterface dialog, int id) 
      { 
       String data = ""; 
       try 
       { 
      FileInputStream fis = openFileInput("authfile");       InputStreamReader in = new InputStreamReader(fis); 
       BufferedReader br = new BufferedReader(in); 
       data = br.readLine(); 
       } 
       catch (Exception e) 
       { 
        e.printStackTrace(); 
       } 
      if (data.toString().equals(passInput.getText().toString())) 
        { 
         txtv.setText("You Have Logged in"); 
        } 
        else 
        { 
        RetryDialog();//Call the dialog of retry 
        } 
       } 
      }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() 
      { 
       public void onClick(DialogInterface dialog, int id) 
       { 
        dialog.cancel(); 
       } 
      }); 
    // create alert dialog 
    AlertDialog alertDialog = alertDialogBuilder.create(); 
    // show it 
    alertDialog.show(); 
} 
//Shows the Retry Dialog 
private void RetryDialog() 
{ 
    LayoutInflater ln = LayoutInflater.from(context); 
    View invalidView = ln.inflate(R.layout.activity_main, null); 
    AlertDialog.Builder invalidDialogBuild = new AlertDialog.Builder(context); 
    // invalidDialogBuild.setView(invalidView); 
    // set dialog message 
    invalidDialogBuild.setCancelable(false) 
      .setPositiveButton("Retry", new DialogInterface.OnClickListener() 
      { 
       public void onClick(DialogInterface dialog, int id) 
       { 
        dialogShow(); 
        dialog.cancel(); 
       } 
      }).setNegativeButton("Reset", new DialogInterface.OnClickListener() 
      { 
       public void onClick(DialogInterface dialog, int id) 
       { 
        dialog.cancel(); 

       } 
      }); 

    AlertDialog invalidDialog = invalidDialogBuild.create(); 
    // show it 
    invalidDialog.show(); 

} 

EDITED:

Dialog với ThreeButtons.

/** 
* This is method to display dialog with three 
* button("Yes, No and Cancel button") 
*/ 
public void showThreeButtonDialog() 
{ 
    private AlertDialog m_alertDialog; 
    private AlertDialog.Builder m_builder = new AlertDialog.Builder(this); 
    m_builder.setTitle("Title Text"); 
    m_builder.setMessage("Dialog Message"); 
    m_builder.setPositiveButton("yes", new DialogInterface.OnClickListener(){ 
     @Override 
     public void onClick(DialogInterface p_dialog, int p_which) 
     { 
      Toast.makeText(AlertDialogActivity.this, "Press Yes", Toast.LENGTH_SHORT).show(); 
     } 
    }); 
    m_builder.setNeutralButton("No"), new DialogInterface.OnClickListener(){ 
     @Override 
     public void onClick(DialogInterface p_dialog, int p_which) 
     { 
      Toast.makeText(AlertDialogActivity.this, "Press No", Toast.LENGTH_SHORT).show(); 
     } 
    }); 
    m_builder.setNegativeButton("Cancel"), new DialogInterface.OnClickListener(){ 
     @Override 
     public void onClick(DialogInterface p_dialog, int p_which) 
     { 
      Toast.makeText(AlertDialogActivity.this, "Press Cancel", Toast.LENGTH_SHORT).show(); 
     } 
    }); 
    m_alertDialog = m_builder.create(); 
    m_alertDialog.show(); 
} 
+0

đã nhận được nó.Cảm ơn một tấn – GunJack

+0

Bạn đang chào đón thân mến ...... :) – GrIsHu

+0

Một truy vấn nữa, là nó pos có thể có 3 nút trong alertDialog. – GunJack

1

Grishu đánh bại tôi vào nó, tôi là ít nhất ngắn hơn ...

private void showLoginDialog(final Context context) { 
    LayoutInflater li = LayoutInflater.from(context); 
    View passView = li.inflate(R.layout.authdialog, null); 
    final EditText passInput = (EditText) passView.findViewById(R.id.editTextDialogUserInput); 
    final TextView txtv = (TextView) findViewById(R.id.textv); 

    new AlertDialog.Builder(context) 
     .setView(passView) 
     .setCancelable(false) 
     .setPositiveButton("OK", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int id) { 
       String data = ""; 
       try { 
        FileInputStream fis = openFileInput("authfile"); 
         InputStreamReader in = new InputStreamReader(fis); 
        BufferedReader br = new BufferedReader(in); 
        data = br.readLine(); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 

       if (data.toString().equals(passInput.getText().toString())) { 
        txtv.setText("You Have Logged in"); 
       } else { 
        dialog.dismiss(); 
        showInvalidDialog(context); 
       } 
      } 
     }) 
     .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int id) { 
       dialog.dismiss(); 
      } 
     }) 
     .show(); 
} 

private void showInvalidDialog(final Context context) { 
    LayoutInflater ln = LayoutInflater.from(context); 
    View invalidView = ln.inflate(R.layout.invdialog, null); 

    new AlertDialog.Builder(context) 
     .setView(invalidView) 
     .setCancelable(false) 
     .setPositiveButton("Retry", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int id) { 
       dialog.dismiss(); 
       showLoginDialog(context); 
      } 
     }) 
     .setNegativeButton("Reset", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int id) { 
       dialog.dismiss(); 
      } 
     }) 
     .show(); 
} 

Giải thích: di chuyển mã cho hai hộp thoại vào phương pháp riêng biệt mà có thể gọi cho nhau. Sử dụng hậu quả của mẫu trình xây dựng để giữ mã ngắn và thanh lịch. Sử dụng dialog.dismiss() thay vì dialog.cancel() trừ khi bạn muốn một OnCancelListener tiềm năng được gọi.

+0

Cảm ơn, tôi đã nhận được lừa ngay bây giờ. – GunJack

Các vấn đề liên quan