如果语句和AlertDialog框

时间:2013-01-31 03:57:22

标签: android android-alertdialog

我有这个应用程序,当您登录时,您将看到名称之类的列表。我也有一个添加,更新和删除按钮。当我点击编辑按钮时,它将转到编辑页面,您可以在那里编辑名称。现在,当您点击“更新”按钮时,我有一个AlertDialog,它将再次询问用户的用户名和密码。这是我制作的代码:

public void btn_add_update_click(View v){
    hideKeyboard();
    final String username = getIntent().getStringExtra("USERNAME");
    final String str_sitename = this.edt_sitename.getText().toString();
    String str_username = this.edt_username.getText().toString();
    String str_password = this.edt_password.getText().toString();
    LayoutInflater li = LayoutInflater.from(context);
    View prompt = li.inflate(R.layout.prompts, null);
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
    alertDialogBuilder.setView(prompt);
    final EditText user = (EditText) prompt.findViewById(R.id.loginEmail);
    final EditText pass = (EditText) prompt.findViewById(R.id.loginPassword);
    final TextView msg = (TextView) prompt.findViewById(R.id.login_error);
    final String password = pass.getText().toString();
    user.setText(getIntent().getStringExtra("USERNAME"));


    if (str_sitename.equals("")){
        //edt_sitename.requestFocus();
        show_mesg("Please insert sitename.");
    }else if (str_username.equals("")){
        //edt_username.requestFocus();
        show_mesg("Please insert username.");
    }else if (str_password.equals("")){
        //edt_password.requestFocus();
        show_mesg("Please insert password.");
    }else{
        if (selected_website!=null){
            selected_website.setUser(username);
            selected_website.setSitename(str_sitename);
            selected_website.setUsername(str_username);
            selected_website.setPassword(str_password);
                alertDialogBuilder.setCancelable(false)
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {    
                        DBAdapter dbUser = new DBAdapter(PasswordActivity.this);
                        dbUser.open();
                        if(dbUser.Login(username, password))
                        {
                                datasource.updateWebsite(selected_website);
                                show_mesg(str_sitename + " updated.");
                                hideKeyboard();
                                selected_website = null;
                                show_list_layout(); 
                            }
                        else{
                            msg.setText("Username or Password is not correct.");
                            }
                        }
                })
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();

                    }
                });
                AlertDialog alertDialog = alertDialogBuilder.create();
                alertDialog.show(); 
        }

        else{
            datasource.addWebsite(username, str_sitename,str_username,str_password);
            hideKeyboard();
            show_mesg(str_sitename + " added.");
            selected_website = null;
            show_add_layout();
        }
    }
}

但是当我运行它时,它不起作用。 “确定”和“取消”按钮未显示。 对此有何建议?

1 个答案:

答案 0 :(得分:1)

首先,您确定您的控制权已进入更新状态吗?如果是这样,在创建警报对话框时,代码中似乎没有任何缺陷。无论如何,如果您确定您的控件处于更新状态,则值得尝试此操作。

 void showUpdateDialog(){
      AlertDialog.Builder updateDialog  = new AlertDialog.Builder(YourClassName.this);
      updateDialog.setTitle("Update Dialog");
      updateDialog.setCancelable(false);

    LayoutInflater layoutInflater  = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

  View inflatingView=layoutInflater.inflate(R.layout.updatelayout,null);

      EditText user = (EditText) inflatingView.findViewById(R.id.loginEmail);
      EditText pass = (EditText) inflatingView.findViewById(R.id.loginPassword);

      // More views here.......

 updateDialog.setPositiveButton("OK", new DialogInterface.OnClickListener(){

   @Override
   public void onClick(DialogInterface arg0, int arg1) {
    // TODO Auto-generated method stub

   }});

  updateDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){

   @Override
   public void onClick(DialogInterface arg0, int arg1) {
    // TODO Auto-generated method stub

   }});

        updateDialog.setView(inflatingView);
        updateDialog.show();
    }

在您的活动中点击更新按钮时,请调用此showUpdateDialog()方法。如果仍然找不到正面和负面按钮,请创建自己的按钮。然后,您不需要依赖系统的正负按钮。在updatelayout.xml中添加两个额外的按钮,并在showUpdateDialog()方法中对其进行编码,并使用这些按钮执行任何操作。

Button yesButton = (Button)view.findViewById(R.id.positiveButton);
  yesButton.setOnClickListener(new Button.OnClickListener(){

    @Override
       public void onClick(View arg0) {
        // TODO Auto-generated method stub
        Toast.makeText(getApplicationContext(), " Yes Button Is Pressed", Toast.LENGTH_LONG).show();
       }});