如何保持Dialog打开

时间:2016-10-09 16:08:10

标签: android alertdialog

private void openDialog(){

    LayoutInflater inflater = LayoutInflater.from(TrueAct.this);
    View subView = inflater.inflate(R.layout.newdialog, null);
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Enter New");
    builder.setView(subView);

    blEntryExistToday = true;
    builder.setPositiveButton("ADD", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            if (!blEntryExistToday) {
                //CLOSE DIALOG
            }
            else {
                tvM.setText("An entry for this day already exist!");
                //DO NOT CLOSE DIALOG
            }
        }
    });

    builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(MainActivity.this, "Cancel", Toast.LENGTH_LONG).show();
            //CLOSE THE DIALOG
        }
    });

    builder.show();
}

无论是单击“添加”还是“取消”,对话框都会关闭。如果blEntryExistTodaytrue,我该如何将其保持打开状态。

enter image description here

我想保持与现在相同的主题,颜色和文字。

4 个答案:

答案 0 :(得分:2)

请勿致电builder.setPositiveButton(..)builder.setNegativeButton(..)。而是向R.layout.newdialog添加按钮并处理这些按钮的点击。如果希望关闭对话框,请致电dialog.dismiss()

要获得与内置按钮相同的外观,只需使用style="?android:borderlessButtonStyle"android:textColor="?android:colorAccent"

答案 1 :(得分:2)

您可以在构建 AlertDialog 时停止设置侦听器,并为正负按钮设置侦听器 null ,并自行处理点击。

您可以通过以下方式更改代码:

 <?php
    error_reporting(0);
    include("config.php");
    $host = "localhost"; //DB host 
$username = "root";  //DB Username 
$password = ""; //DB Password 
$db_name = "hklcanet_pha"; //DB Name 
$tbl_name = "users"; //Table name, where users are stored 
$dbconfig = mysqli_connect($host,$username,$password,$db_name);



$username = $_POST['username']; //Get username from login form 
$password = $_POST['password']; //Get password from login form 

$username = stripslashes($username); //Makes string safe 
$password = stripslashes($password); //Makes string safe 
$username = mysqli_real_escape_string($dbconfig, $username); //Makes string safer 
$password = mysqli_real_escape_string($dbconfig, $password); //Makes string safer 

$sql = "SELECT * FROM $tbl_name WHERE username='$username' and password='$password'"; //SQL Query 
$result = mysqli_query($dbconfig, $sql); //Executes Query 

$rows = mysqli_num_rows($result); //Count rows selected (1 if a username/password combo can be found) 

if($rows == 1){ 
        session_start(); //Starts a PHP session 
        $_SESSION['username'] = $username; //Allows $username to be used later 
            header("location: interphase1.php");
        $query  = "SELECT * FROM $tbl_name WHERE username='$username' and password='$password'"; 
        $result = mysqli_query($dbconfig, $query); 
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) 
        { 
        $permissions = $row['permissions']; //Gets the permissions of the user 
        $id = $row['id']; //Gets the ID of the user 
        } 
        $_SESSION['permissions'] = $permissions; //Allows $permissions to be used later 
        $_SESSION['id'] = $id; //Allows $id to be used later 
        $_SESSION['authenticated'] = 1; //Allows $id to be used later 

echo("Login Succesful");//Prints success message 
} 
else  
{ 
//echo("Invalid Username/Password"); 
} 
?>

答案 2 :(得分:1)

这是AlertDialog的通常行为。以一种简单的方式,你无法避免。但是你得到了一些可能的解决方法:

自API8起,您可以设置onShowListener()(在致电builder.create()后):

    builder.setOnShowListener(new DialogInterface.OnShowListener(){

           @Override
        onShow(DialogInterface interface){

        Button positiveButton = builder.getButton(AlertDialog.BUTTON_POSITIVE);
          positiveButton.setOnClickListener(new OnClickListener()){

                @Override
                public void OnClick(View view){

             if (!blEntryExistToday) {
            //CLOSE DIALOG
            builder.dismiss();

                 } else {

            tvM.setText("An entry for this day already exist!");
            //DO NOT CLOSE DIALOG
             // if you doesn´t call builder.dismiss(); it won´t close
                   }                     
                  }
                });    
              }
            });

或类似的方法,在没有builder.getButton()的情况下调用builder.show()后调用onShowListener()方法。

答案 3 :(得分:1)

我的情况有点不同。最好不要求添加自定义按钮并保持setPositiveButton的使用。所以:

// A function to create the dialog
public static void inputText( Context context, String title, String hint, String posBtn, String negBtn, DialogInterface.OnClickListener listener )
{
    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setTitle( title );

    LinearLayout layout = (LinearLayout) View.inflate( context, R.layout.dialog_input_text, null );
    final EditText input = (EditText) layout.findViewById( R.id.input );
    input.setHint( hint );
    builder.setView( layout );

    builder.setPositiveButton( posBtn, listener );
    builder.setNegativeButton( negBtn, new DialogInterface.OnClickListener()
    {
        @Override
        public void onClick( DialogInterface dialog, int which )
        {
            dialog.cancel();
        }
    } );

    final Dialog dialog = builder.show();
}

在这个对话框的所有用法中,我都像上面这样调用上面的函数:

DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener()
{
    @Override
    public void onClick( DialogInterface dialogInterface, int i )
    {
        final AlertDialog dialog = (AlertDialog) dialogInterface;

        EditText field = (EditText) dialog.findViewById( R.id.input );
        String name = field.getText().toString();

        if( name.equals( "" ) )
        {
            Toast.makeText( MainActivity.this, "Fill with a name.", Toast.LENGTH_SHORT ).show();
            Dialogs.inputText( MainActivity.this, "Rename", longClickFileObject.name, "Ok", "Cancel", this );
        }
        else
        {
            // Do stuff
        }
    }
};

inputText( this, "Rename", longClickFileObject.name, "Ok", "Cancel", listener );

它没有使用自定义按钮,但它仍然很容易阅读,但它必须重新创建对话框并使其闪烁。