ඇන්ඩ්‍රොයිඩ් හි ඇඟවීම් සංවාදයක් පෙන්වන්නේ කෙසේද?


1091

පරිශීලකයාට "මෙම ප්‍රවේශය මකා දැමීමට ඔබට විශ්වාසද?" යනුවෙන් පෙන්වන පණිවිඩයක් සහිත සංවාද / උත්පතන කවුළුවක් ප්‍රදර්ශනය කිරීමට මට අවශ්‍යය. 'මකන්න' යැයි පවසන එක් බොත්තමක් සමඟ. Deleteස්පර්ශ කළ විට , එය එම ප්‍රවේශය මකා දැමිය යුතුය, එසේ නොමැතිනම් කිසිවක් නැත.

මම එම බොත්තම් සඳහා ක්ලික් සවන්දෙන්නෙකු ලියා ඇත, නමුත් මම සංවාදයක් හෝ උත්පතන සහ එහි ක්‍රියාකාරිත්වය ඉල්ලා සිටින්නේ කෙසේද?



ඔබ ද්‍රව්‍යමය සංවාද පුස්තකාලය භාවිතා නොකරන්නේ ඇයි?
විවේක්_නීල්

1
එකක්, දෙකක් සහ බොත්තම් ඇඟවීම් උදාහරණ සඳහා, මෙම පිළිතුර බලන්න .
සුරග්

Answers:


1828

ඔබට AlertDialogමේ සඳහා a භාවිතා කර එහි Builderපන්තිය භාවිතයෙන් එකක් සෑදිය හැකිය . පහත උදාහරණයෙන් පෙරනිමි ඉදිකිරීම්කරු භාවිතා කරයි, Contextමන්දයත් ඔබ විසින් ගෙන යන සන්දර්භයෙන් සංවාදයට නිසි තේමාව උරුම වන හෙයිනි, නමුත් ඔබට කිරීමට අවශ්‍ය නම් දෙවන පරාමිතිය ලෙස නිශ්චිත තේමා සම්පතක් නියම කිරීමට ඔබට ඉඩ සලසන ඉදිකිරීම්කරුවෙකු ද ඇත. නිසා.

new AlertDialog.Builder(context)
    .setTitle("Delete entry")
    .setMessage("Are you sure you want to delete this entry?")

    // Specifying a listener allows you to take an action before dismissing the dialog.
    // The dialog is automatically dismissed when a dialog button is clicked.
    .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) { 
            // Continue with delete operation
        }
     })

    // A null listener allows the button to dismiss the dialog and take no further action.
    .setNegativeButton(android.R.string.no, null)
    .setIcon(android.R.drawable.ic_dialog_alert)
    .show();

34
නො කළ යුතු AlertDialog.Builder(this)මඟින් ප්රතිස්ථාපනය කළ AlertDialog.Builder(className.this)?
අපූර්වා

23
අවශ්‍ය නොවේ. ඔබ යම් සවන්දෙන්නෙකුගෙන් අනතුරු ඇඟවීමේ සංවාදයක් ගොඩනගන්නේ නම් එය අවශ්‍ය වේ.
ඇල්ෆා

5
AlertDialog මතක තබා ගන්න. සේවයෙන් ඉවත් කිරීමේ () ක්‍රමය මඟින් බිල්ඩර් ඉවත් කළ නොහැක. ඔබට විකල්පයක් ලෙස භාවිතා කළ හැකිය AlertDialog dialog = නව AlertDialog.Builder (සන්දර්භය) .Create (); සාමාන්‍යයෙන් ඔබට එය ඉවත් කිරීම () ලෙස හැඳින්විය හැක.
ෆස්ටිගඩෝර්

2
ලාච්චු අයිතමය තෝරාගැනීමේදී වැඩ නොකළ නමුත් මෙය සිදු කළේ: stackoverflow.com/a/26097588/1953178
අම්ර් හොසම්

5
සත්‍ය නොවේ @ ෆස්ටිගඩෝර්
අජේ

354

මෙම කේතය උත්සාහ කරන්න:

AlertDialog.Builder builder1 = new AlertDialog.Builder(context);
builder1.setMessage("Write your message here.");
builder1.setCancelable(true);

builder1.setPositiveButton(
    "Yes",
    new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            dialog.cancel();
        }
    });

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

AlertDialog alert11 = builder1.create();
alert11.show();

4
+1. මෙය කිරීමට වඩා හොඳ ක්‍රමයකි. A මහේෂ් විසින් සංවාදයේ නිදසුනක් නිර්මාණය කර ඇති අතර එම නිසා එය ප්‍රවේශ විය හැකිය cancel().
Subby

2
වන builder1.create()ඔබ ආරාධනා එය වැඩ දඩ පෙනේ නිසා අවශ්ය builder1.show()සෘජුවම?
razz

2
zrazzak ඔව් එය අවශ්‍ය වන්නේ එය අපට සංවාද අවස්ථාවක් සපයන බැවිනි. සංවාද විශේෂිත ක්‍රමයට ප්‍රවේශ වීම සඳහා අපට සංවාද නිදසුන භාවිතා කළ හැකිය
මහේෂ්

1
මම මෙම ක්‍රමය උත්සාහ කරමි, නමුත් එය කියවීමට මට කාලය නොදී අනතුරු ඇඟවීමේ කවුළුව මතු වී වහාම අතුරුදහන් වේ. එය ඉවත් කිරීමට බොත්තම් මත ක්ලික් කිරීමට මට වෙලාවක් නැත. කිසියම් අදහසක් ඇයි?
lweingart


100

ඩේවිඩ් හෙඩ්ලන්ඩ් විසින් පළ කරන ලද කේතය මට දෝෂයක් ලබා දුන්නේය:

කවුළුව එක් කළ නොහැක - ටෝකන ශුන්‍යය වලංගු නොවේ

ඔබට එකම දෝෂයක් ලැබෙන්නේ නම් පහත කේතය භාවිතා කරන්න. එය වැඩ කරනවා!!

runOnUiThread(new Runnable() {
    @Override
    public void run() {

        if (!isFinishing()){
            new AlertDialog.Builder(YourActivity.this)
              .setTitle("Your Alert")
              .setMessage("Your Message")
              .setCancelable(false)
              .setPositiveButton("ok", new OnClickListener() {
                  @Override
                  public void onClick(DialogInterface dialog, int which) {
                      // Whatever...
                  }
              }).show();
        }
    }
});

3
මේ දෙකම භාවිතා කිරීමට අපට අවශ්‍ය නොවන create()අතර show(), show()දැනටමත් විස්තර කර ඇති අන්තර්ගතය සමඟ සංවාදය නිර්මාණය කරයි. ප්‍රලේඛනයට අනුව, create() මෙම තනන්නාට සපයා ඇති තර්ක සමඟ අනතුරු ඇඟවීමේ සංවාදයක් සාදයි. එය Dialog.show () දෙබස නොවේ. සංවාදය පෙන්වීමට පෙර පරිශීලකයාට අමතර සැකසුම් කිරීමට මෙය ඉඩ දෙයි. ඔබට වෙනත් සැකසුම් කිරීමට නොමැති නම් සහ මෙය නිර්මාණය කර ප්‍රදර්ශනය කිරීමට අවශ්‍ය නම් පෙන්වන්න () භාවිතා කරන්න. එබැවින් එය ප්‍රයෝජනවත් create()වන්නේ ඔබ පසුව සංවාදය පෙන්වීමට සැලසුම් කරන්නේ නම් පමණක් වන අතර ඔබ එහි අන්තර්ගතය කල්තියා පටවනු ලැබේ.
රුචීර් බාරෝනියා

1
පරාමිතිය සිට වෙනස් getApplicationContext()කර MyActivity.thisවැඩ කිරීමට පටන් ගත්තේය.
ඕ -9

70

සරල එකක්! ඔබේ ජාවා පන්තියේ ඕනෑම තැනක සංවාද ක්‍රමයක් සාදන්න:

public void openDialog() {
    final Dialog dialog = new Dialog(context); // Context, this, etc.
    dialog.setContentView(R.layout.dialog_demo);
    dialog.setTitle(R.string.dialog_title);
    dialog.show();
}

දැන් පිරිසැලසුම් XML dialog_demo.xmlනිර්මාණය කර ඔබේ UI / සැලසුම සාදන්න . නිරූපණ අරමුණු සඳහා මා විසින් සාදන ලද නියැදියක් මෙන්න:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/dialog_info"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="@string/dialog_text"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_below="@id/dialog_info">

        <Button
            android:id="@+id/dialog_cancel"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.50"
            android:background="@color/dialog_cancel_bgcolor"
            android:text="Cancel"/>

        <Button
            android:id="@+id/dialog_ok"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.50"
            android:background="@color/dialog_ok_bgcolor"
            android:text="Agree"/>
    </LinearLayout>
</RelativeLayout>

දැන් ඔබට කැමති openDialog()ඕනෑම තැනක සිට ඇමතිය හැකිය :) මෙන්න ඉහත කේතයේ තිර රුව.

රූප විස්තරය මෙහි ඇතුළත් කරන්න

පෙළ සහ වර්ණය භාවිතා කරන්නේ strings.xmlසහ සිට බව සලකන්න colors.xml. ඔබට ඔබේම අර්ථ දැක්විය හැකිය.


4
ඩයලොග් පංතිය යනු සංවාද සඳහා මූලික පන්තිය වන නමුත් ඔබ කෙලින්ම ඩයලොග් ක්ෂණිකව වළක්වා ගත යුතුය . ඒ වෙනුවට, පහත සඳහන් උප වර්ගිකරණයන් භාවිතය එක්: AlertDialog, DatePickerDialog or TimePickerDialog(සිට developer.android.com/guide/topics/ui/dialogs.html )
sweisgerber.dev

"අවලංගු කරන්න" සහ "එකඟ වන්න" මෙහි ක්ලික් කළ නොහැක.
c1ph4

65

AlertDialog.Builder භාවිතා කරන්න :

AlertDialog alertDialog = new AlertDialog.Builder(this)
//set icon 
 .setIcon(android.R.drawable.ic_dialog_alert)
//set title
.setTitle("Are you sure to Exit")
//set message
.setMessage("Exiting will call finish() method")
//set positive button
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialogInterface, int i) {
   //set what would happen when positive button is clicked    
        finish();
    }
})
//set negative button
.setNegativeButton("No", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialogInterface, int i) {
   //set what should happen when negative button is clicked
        Toast.makeText(getApplicationContext(),"Nothing Happened",Toast.LENGTH_LONG).show();
    }
})
.show();

ඔබට පහත ප්‍රතිදානය ලැබෙනු ඇත.

android ඇඟවීම් සංවාදය

ඇඟවීම් සංවාද නිබන්ධනය බැලීමට පහත සබැඳිය භාවිතා කරන්න.

Android Alert Dialog Tutorial


54

වර්තමානයේ A ජු ඇලර්ට් ඩයලොග් නිර්මාණය වෙනුවට ඩයලොග් ෆ්‍රැග්මන්ට් භාවිතා කිරීම වඩා හොඳය.


1
ප්ලස් මගේ අභිරුචි අන්තර්ගත දර්ශනය සමඟ එය අවුලුවන විට අමුතු පද්ධතියක් වන ඇලර්ට් ඩයලොග් පසුබිමෙන් මිදීමට උත්සාහ කිරීමේදී මට බොහෝ කරදර ඇති විය.
goRGon

44

ඔබට මෙම කේතය භාවිතා කළ හැකිය:

AlertDialog.Builder alertDialog2 = new AlertDialog.Builder(
    AlertDialogActivity.this);

// Setting Dialog Title
alertDialog2.setTitle("Confirm Delete...");

// Setting Dialog Message
alertDialog2.setMessage("Are you sure you want delete this file?");

// Setting Icon to Dialog
alertDialog2.setIcon(R.drawable.delete);

// Setting Positive "Yes" Btn
alertDialog2.setPositiveButton("YES",
    new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            // Write your code here to execute after dialog
            Toast.makeText(getApplicationContext(),
                           "You clicked on YES", Toast.LENGTH_SHORT)
                    .show();
        }
    });

// Setting Negative "NO" Btn
alertDialog2.setNegativeButton("NO",
    new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            // Write your code here to execute after dialog
            Toast.makeText(getApplicationContext(),
                           "You clicked on NO", Toast.LENGTH_SHORT)
                    .show();
            dialog.cancel();
        }
    });

// Showing Alert Dialog
alertDialog2.show();

1
dialog.cancel (); දෙවන සවන්දෙන්නෙකු කැඳවිය යුතු නැත
demaksee

"මෙම නිබන්ධනය" සබැඳිය කැඩී ඇත. එය ඔබව " store.hp.com/… " වෙත ගෙන යයි
ජේම්ස් ඩීහාර්ට්

39

මට ද

new AlertDialog.Builder(this)
    .setTitle("Closing application")
    .setMessage("Are you sure you want to exit?")
    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int which) {

          }
     }).setNegativeButton("No", null).show();

33
// Dialog box

public void dialogBox() {
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
    alertDialogBuilder.setMessage("Click on Image for tag");
    alertDialogBuilder.setPositiveButton("Ok",
        new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface arg0, int arg1) {
        }
    });

    alertDialogBuilder.setNegativeButton("cancel",
        new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface arg0, int arg1) {

        }
    });

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

1
Yout කේතය වැරදියි, ඔබ setPositiveButton ("අවලංගු කරන්න" setNegativeButton ලෙස වෙනස් කළ යුතුය ("අවලංගු කරන්න"
Benoist Laforge

ස්තූතියි, එය වැරදීමකින් සිදු විය ... ඇත්ත වශයෙන්ම මට ඕනෑම කෙනෙකුට පළ කරන ලද කේතය ගැඹුරින් පරීක්ෂා කළ හැකිද නැද්ද යන්න පරීක්ෂා කිරීමට අවශ්‍යය. ඒ වගේම ඔබයි ... නැවතත් ස්තූතියි ..
අනිල් සිංහානියා

31
new AlertDialog.Builder(context)
    .setTitle("title")
    .setMessage("message")
    .setPositiveButton(android.R.string.ok, null)
    .show();

23

මෙය ඇඟවීම් සංවාදයක් නිර්මාණය කරන්නේ කෙසේද යන්න පිළිබඳ මූලික නියැදියකි :

AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
dialog.setCancelable(false);
dialog.setTitle("Dialog on Android");
dialog.setMessage("Are you sure you want to delete this entry?" );
dialog.setPositiveButton("Delete", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int id) {
        //Action for "Delete".
    }
})
        .setNegativeButton("Cancel ", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
            //Action for "Cancel".
            }
        });

final AlertDialog alert = dialog.create();
alert.show();

රූප විස්තරය මෙහි ඇතුළත් කරන්න


15

මෙය නියත වශයෙන්ම ඔබට උපකාරයකි. මෙම කේතය උත්සාහ කරන්න: බොත්තමක් ක්ලික් කිරීමෙන් ඔබට බොත්තම් එකක්, දෙකක් හෝ තුනක් අනතුරු ඇඟවීමේ සංවාදයකින් තැබිය හැකිය ...

SingleButtton.setOnClickListener(new View.OnClickListener() {

    public void onClick(View arg0) {
        // Creating alert Dialog with one Button

        AlertDialog alertDialog = new AlertDialog.Builder(AlertDialogActivity.this).create();

        // Setting Dialog Title
        alertDialog.setTitle("Alert Dialog");

        // Setting Dialog Message
        alertDialog.setMessage("Welcome to Android Application");

        // Setting Icon to Dialog
        alertDialog.setIcon(R.drawable.tick);

        // Setting OK Button
        alertDialog.setButton("OK", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog,int which)
            {
                // Write your code here to execute after dialog    closed
                Toast.makeText(getApplicationContext(),"You clicked on OK", Toast.LENGTH_SHORT).show();
            }
        });

        // Showing Alert Message
        alertDialog.show();
    }
});

btnAlertTwoBtns.setOnClickListener(new View.OnClickListener() {

    public void onClick(View arg0) {
        // Creating alert Dialog with two Buttons

        AlertDialog.Builder alertDialog = new AlertDialog.Builder(AlertDialogActivity.this);

        // Setting Dialog Title
        alertDialog.setTitle("Confirm Delete...");

        // Setting Dialog Message
        alertDialog.setMessage("Are you sure you want delete this?");

        // Setting Icon to Dialog
        alertDialog.setIcon(R.drawable.delete);

        // Setting Positive "Yes" Button
        alertDialog.setPositiveButton("YES",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,int which) {
                        // Write your code here to execute after dialog
                        Toast.makeText(getApplicationContext(), "You clicked on YES", Toast.LENGTH_SHORT).show();
                    }
                });

        // Setting Negative "NO" Button
        alertDialog.setNegativeButton("NO",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,    int which) {
                        // Write your code here to execute after dialog
                        Toast.makeText(getApplicationContext(), "You clicked on NO", Toast.LENGTH_SHORT).show();
                        dialog.cancel();
                    }
                });

        // Showing Alert Message
        alertDialog.show();
    }
});

btnAlertThreeBtns.setOnClickListener(new View.OnClickListener() {

    public void onClick(View arg0) {
        // Creating alert Dialog with three Buttons

        AlertDialog.Builder alertDialog = new AlertDialog.Builder(
                AlertDialogActivity.this);

        // Setting Dialog Title
        alertDialog.setTitle("Save File...");

        // Setting Dialog Message
        alertDialog.setMessage("Do you want to save this file?");

        // Setting Icon to Dialog
        alertDialog.setIcon(R.drawable.save);

        // Setting Positive Yes Button
        alertDialog.setPositiveButton("YES",
            new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog,
                        int which) {
                    // User pressed Cancel button. Write Logic Here
                    Toast.makeText(getApplicationContext(),
                            "You clicked on YES",
                            Toast.LENGTH_SHORT).show();
                }
            });

        // Setting Negative No Button... Neutral means in between yes and cancel button
        alertDialog.setNeutralButton("NO",
            new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog,
                        int which) {
                    // User pressed No button. Write Logic Here
                    Toast.makeText(getApplicationContext(),
                            "You clicked on NO", Toast.LENGTH_SHORT)
                            .show();
                }
            });

        // Setting Positive "Cancel" Button
        alertDialog.setNegativeButton("Cancel",
            new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog,
                        int which) {
                    // User pressed Cancel button. Write Logic Here
                    Toast.makeText(getApplicationContext(),
                            "You clicked on Cancel",
                            Toast.LENGTH_SHORT).show();
                }
            });
        // Showing Alert Message
        alertDialog.show();
    }
});

14

පුද්ගලයෙකුට පුද්ගලයෙකු ඇමතීමට අවශ්‍යද නැද්ද යන්න විමසීමට මම සංවාදයක් නිර්මාණය කර ඇත්තෙමි.

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.Toast;

public class Firstclass extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.first);

        ImageView imageViewCall = (ImageView) findViewById(R.id.ring_mig);

        imageViewCall.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v)
            {
                try
                {
                    showDialog("0728570527");
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
            }
        });
    }

    public void showDialog(final String phone) throws Exception
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(Firstclass.this);

        builder.setMessage("Ring: " + phone);

        builder.setPositiveButton("Ring", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                Intent callIntent = new Intent(Intent.ACTION_DIAL);// (Intent.ACTION_CALL);

                callIntent.setData(Uri.parse("tel:" + phone));

                startActivity(callIntent);

                dialog.dismiss();
            }
        });

        builder.setNegativeButton("Avbryt", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                dialog.dismiss();
            }
        });

        builder.show();
    }
}

14

ඔබට මෙය උත්සාහ කළ හැකිය ....

    AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
dialog.setCancelable(false);
dialog.setTitle("Dialog on Android");
dialog.setMessage("Are you sure you want to delete this entry?" );
dialog.setPositiveButton("Delete", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int id) {
        //Action for "Delete".
    }
})
        .setNegativeButton("Cancel ", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
            //Action for "Cancel".
            }
        });

final AlertDialog alert = dialog.create();
alert.show();

වැඩි විස්තර සඳහා, මෙම සබැඳිය පරීක්ෂා කරන්න ...


11

ඔබට සංවාද කොටුව නිර්මාණය කළ හැකිය AlertDialog.Builder

මේක උත්සාහ කරන්න:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("Are you sure you want to delete this entry?");

        builder.setPositiveButton("Yes, please", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //perform any action
                Toast.makeText(getApplicationContext(), "Yes clicked", Toast.LENGTH_SHORT).show();
            }
        });

        builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //perform any action
                Toast.makeText(getApplicationContext(), "No clicked", Toast.LENGTH_SHORT).show();
            }
        });

        //creating alert dialog
        AlertDialog alertDialog = builder.create();
        alertDialog.show();

ඇඟවීම් සංවාදයේ ධනාත්මක හා සෘණ බොත්තම් වල වර්ණය වෙනස් කිරීම සඳහා ඔබට පහත පේළි දෙක පසුව ලිවිය හැකිය alertDialog.show();

alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(getResources().getColor(R.color.colorPrimary));
alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(getResources().getColor(R.color.colorPrimaryDark));

රූප විස්තරය මෙහි ඇතුළත් කරන්න


11

මෙම කේතය උත්සාහ කරන්න

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);

    // set title
    alertDialogBuilder.setTitle("AlertDialog Title");

    // set dialog message
    alertDialogBuilder
            .setMessage("Some Alert Dialog message.")
            .setCancelable(false)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                            Toast.makeText(this, "OK button click ", Toast.LENGTH_SHORT).show();

                }
            })
            .setNegativeButton("CANCEL",new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                           Toast.makeText(this, "CANCEL button click ", Toast.LENGTH_SHORT).show();

                    dialog.cancel();
                }
            });

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

    // show it
    alertDialog.show();

10
showDialog(MainActivity.this, "title", "message", "OK", "Cancel", {...}, {...});

කොට්ලින්

fun showDialog(context: Context, title: String, msg: String,
               positiveBtnText: String, negativeBtnText: String?,
               positiveBtnClickListener: DialogInterface.OnClickListener,
               negativeBtnClickListener: DialogInterface.OnClickListener?): AlertDialog {
    val builder = AlertDialog.Builder(context)
            .setTitle(title)
            .setMessage(msg)
            .setCancelable(true)
            .setPositiveButton(positiveBtnText, positiveBtnClickListener)
    if (negativeBtnText != null)
        builder.setNegativeButton(negativeBtnText, negativeBtnClickListener)
    val alert = builder.create()
    alert.show()
    return alert
}

ජාවා

public static AlertDialog showDialog(@NonNull Context context, @NonNull String title, @NonNull String msg,
                                     @NonNull String positiveBtnText, @Nullable String negativeBtnText,
                                     @NonNull DialogInterface.OnClickListener positiveBtnClickListener,
                                     @Nullable DialogInterface.OnClickListener negativeBtnClickListener) {
    AlertDialog.Builder builder = new AlertDialog.Builder(context)
            .setTitle(title)
            .setMessage(msg)
            .setCancelable(true)
            .setPositiveButton(positiveBtnText, positiveBtnClickListener);
    if (negativeBtnText != null)
        builder.setNegativeButton(negativeBtnText, negativeBtnClickListener);
    AlertDialog alert = builder.create();
    alert.show();
    return alert;
}

8
   new AlertDialog.Builder(v.getContext()).setMessage("msg to display!").show();

කරුණාකර පැහැදිලි කිරීම
GYaN

5
කරුණාකර පැහැදිලි කිරීමක් නැත. මෙම පිළිතුර පරිපූර්ණ වන අතර “කරුණාකර පැහැදිලි කිරීම කරුණාකර” බොට්ස් සතුටු කිරීමට වචන එකතු කිරීමට දරන ඕනෑම උත්සාහයක් එය වඩාත් නරක අතට හැරෙනු ඇත.
දොන් හැච්

7

ඔබට සංවාදය බැහැර කිරීමට අවශ්‍ය විට ප්‍රවේශම් වන්න - භාවිතා කරන්න dialog.dismiss(). මගේ පළමු උත්සාහයේදී මම භාවිතා කළ dismissDialog(0)(සමහර විට මම යම් ස්ථානයකින් පිටපත් කළ) සමහර විට එය ක්‍රියාත්මක වේ. පද්ධතිය සපයන වස්තුව භාවිතා කිරීම ආරක්ෂිත තේරීමක් සේ පෙනේ.


7

ඩේවිඩ් හෙඩ්ලන්ඩ් ඔහු පළ කළ දෙයට වඩා ගතික ක්‍රමයක් බෙදාගැනීමෙන් හොඳ පිළිතුරක් එක් කිරීමට මම කැමතියි, එවිට ඔබට ඉටු කිරීමට negative ණාත්මක ක්‍රියාමාර්ගයක් ඇති විට එය භාවිතා කළ හැකිය.

private void showAlertDialog(@NonNull Context context, @NonNull String alertDialogTitle, @NonNull String alertDialogMessage, @NonNull String positiveButtonText, @Nullable String negativeButtonText, @NonNull final int positiveAction, @Nullable final Integer negativeAction, @NonNull boolean hasNegativeAction)
{
    AlertDialog.Builder builder;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        builder = new AlertDialog.Builder(context, android.R.style.Theme_Material_Dialog_Alert);
    } else {
        builder = new AlertDialog.Builder(context);
    }
    builder.setTitle(alertDialogTitle)
            .setMessage(alertDialogMessage)
            .setPositiveButton(positiveButtonText, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    switch (positiveAction)
                    {
                        case 1:
                            //TODO:Do your positive action here 
                            break;
                    }
                }
            });
            if(hasNegativeAction || negativeAction!=null || negativeButtonText!=null)
            {
            builder.setNegativeButton(negativeButtonText, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    switch (negativeAction)
                    {
                        case 1:
                            //TODO:Do your negative action here
                            break;
                        //TODO: add cases when needed
                    }
                }
            });
            }
            builder.setIcon(android.R.drawable.ic_dialog_alert);
            builder.show();
}

5

මම මෙය AlertDialogබොත්තම් onClickක්‍රමයෙන් භාවිතා කළෙමි :

button.setOnClickListener(v -> {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    LayoutInflater layoutInflaterAndroid = LayoutInflater.from(this);
    View view2 = layoutInflaterAndroid.inflate(R.layout.cancel_dialog, null);
    builder.setView(view2);
    builder.setCancelable(false);
    final AlertDialog alertDialog = builder.create();
    alertDialog.show();

    view2.findViewById(R.id.yesButton).setOnClickListener(v1 -> onBackPressed());
    view2.findViewById(R.id.nobutton).setOnClickListener(v12 -> alertDialog.dismiss());
});

dialog.xml

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
    android:id="@+id/textmain"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:gravity="center"
    android:padding="5dp"
    android:text="@string/warning"
    android:textColor="@android:color/black"
    android:textSize="18sp"
    android:textStyle="bold"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />


<TextView
    android:id="@+id/textpart2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:gravity="center"
    android:lines="2"
    android:maxLines="2"
    android:padding="5dp"
    android:singleLine="false"
    android:text="@string/dialog_cancel"
    android:textAlignment="center"
    android:textColor="@android:color/black"
    android:textSize="15sp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textmain" />


<TextView
    android:id="@+id/yesButton"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="40dp"
    android:layout_marginTop="5dp"
    android:layout_marginEnd="40dp"
    android:layout_marginBottom="5dp"
    android:background="#87cefa"
    android:gravity="center"
    android:padding="10dp"
    android:text="@string/yes"
    android:textAlignment="center"
    android:textColor="@android:color/black"
    android:textSize="15sp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textpart2" />


<TextView
    android:id="@+id/nobutton"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="40dp"
    android:layout_marginTop="5dp"
    android:layout_marginEnd="40dp"
    android:background="#87cefa"
    android:gravity="center"
    android:padding="10dp"
    android:text="@string/no"
    android:textAlignment="center"
    android:textColor="@android:color/black"
    android:textSize="15sp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/yesButton" />


<TextView
    android:layout_width="match_parent"
    android:layout_height="20dp"
    android:layout_margin="5dp"
    android:padding="10dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/nobutton" />
</androidx.constraintlayout.widget.ConstraintLayout>

කරුණාකර සපයා ඇති කේතය හරියටම කරන්නේ කුමක්ද යන්න පැහැදිලි කිරීමක් සමඟ යාවත්කාලීන කරන්න.
wscourge

4

ඔබට මේ ආකාරයෙන් උත්සාහ කළ හැකිය, එය ඔබට ද්‍රව්‍යමය ශෛලීය සංවාද ලබා දෙනු ඇත

private void showDialog()
{
    String text2 = "<font color=#212121>Medi Notification</font>";//for custom title color

    AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.AppCompatAlertDialogStyle);
    builder.setTitle(Html.fromHtml(text2));

    String text3 = "<font color=#A4A4A4>You can complete your profile now or start using the app and come back later</font>";//for custom message
    builder.setMessage(Html.fromHtml(text3));

    builder.setPositiveButton("DELETE", new DialogInterface.OnClickListener()
    {
        @Override
        public void onClick(DialogInterface dialog, int which)
        {
            toast = Toast.makeText(getApplicationContext(), "DELETE", Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();              
        }
    });

    builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener()
    {
        @Override
        public void onClick(DialogInterface dialog, int which)
        {
            toast = Toast.makeText(getApplicationContext(), "CANCEL", Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
        }
    });
    builder.show();
}

4
public void showSimpleDialog(View view) {
    // Use the Builder class for convenient dialog construction
    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    builder.setCancelable(false);
    builder.setTitle("AlertDialog Title");
    builder.setMessage("Simple Dialog Message");
    builder.setPositiveButton("OK!!!", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int id) {
            //
        }
    })
    .setNegativeButton("Cancel ", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

        }
    });

    // Create the AlertDialog object and return it
    builder.create().show();
}

ඇන්ඩ්‍රොයිඩ් හි ඩයලොග් පිළිබඳ මගේ බ්ලොග් අඩවියද බලන්න, ඔබට මෙහි සියලු විස්තර සොයාගත හැකිය: http://www.fahmapps.com/2016/09/26/dialogs-in-android-part1/ .


4

මෙම ස්ථිතික ක්‍රමය සාදා ඔබට අවශ්‍ය ඕනෑම තැනක එය භාවිතා කරන්න.

public static void showAlertDialog(Context context, String title, String message, String posBtnMsg, String negBtnMsg) {
            AlertDialog.Builder builder = new AlertDialog.Builder(context);
            builder.setTitle(title);
            builder.setMessage(message);
            builder.setPositiveButton(posBtnMsg, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });
            builder.setNegativeButton(negBtnMsg, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });
            AlertDialog dialog = builder.create();
            dialog.show();

        }

3

සංස්කරණ පෙළ සමඟ ඇඟවීම් සංවාදය

AlertDialog.Builder builder = new AlertDialog.Builder(context);//Context is activity context
final EditText input = new EditText(context);
builder.setTitle(getString(R.string.remove_item_dialog_title));
        builder.setMessage(getString(R.string.dialog_message_remove_item));
 builder.setTitle(getString(R.string.update_qty));
            builder.setMessage("");
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.MATCH_PARENT);
            input.setLayoutParams(lp);
            input.setHint(getString(R.string.enter_qty));
            input.setTextColor(ContextCompat.getColor(context, R.color.textColor));
            input.setInputType(InputType.TYPE_CLASS_NUMBER);
            input.setText("String in edit text you want");
            builder.setView(input);
   builder.setPositiveButton(getString(android.R.string.ok),
                (dialog, which) -> {

//Positive button click event
  });

 builder.setNegativeButton(getString(android.R.string.cancel),
                (dialog, which) -> {
//Negative button click event
                });
        AlertDialog dialog = builder.create();
        dialog.show();

2
AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("This is Title");
    builder.setMessage("This is message for Alert Dialog");
    builder.setPositiveButton("Positive Button", (dialog, which) -> onBackPressed());
    builder.setNegativeButton("Negative Button", (dialog, which) -> dialog.cancel());
    builder.show();

මෙය කිසියම් කේත රේඛාවක් සමඟ ඇඟවීම් සංවාදය නිර්මාණය කිරීමට සමාන ක්‍රමයකි.


2

ලැයිස්තුවෙන් ඇතුළත් කිරීමක් මකා දැමීමේ කේතය

 /*--dialog for delete entry--*/
private void cancelBookingAlert() {
    AlertDialog dialog;
    final AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this, R.style.AlertDialogCustom);
    alertDialog.setTitle("Delete Entry");
    alertDialog.setMessage("Are you sure you want to delete this entry?");
    alertDialog.setCancelable(false);

    alertDialog.setPositiveButton("Delete", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
           //code to delete entry
        }
    });

    alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });

    dialog = alertDialog.create();
    dialog.show();
}

මකන්න බොත්තම ක්ලික් කිරීමෙන් ඉහත ක්‍රමය අමතන්න


1

ඇන්කෝ සමඟ ( කොට්ලින් හි සංවර්ධකයින්ගේ නිල පුස්තකාලය), ඔබට සරල භාවිතයක් කළ හැකිය

alert("Alert title").show()

හෝ වඩාත් සංකීර්ණ එකක්:

alert("Hi, I'm Roy", "Have you tried turning it off and on again?") {
    yesButton { toast("Oh…") }
    noButton {}
}.show()

ඇන්කෝ ආනයනය කිරීමට:

implementation "org.jetbrains.anko:anko:0.10.8"

1

මෙය කොට්ලින් වලින් සිදු කෙරේ

val builder: AlertDialog.Builder = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            AlertDialog.Builder(this, android.R.style.Theme_Material_Dialog_Alert)
        } else {
            AlertDialog.Builder(this)
        }
        builder.setTitle("Delete Alert!")
                .setMessage("Are you want to delete this entry?")
                .setPositiveButton("YES") { dialog, which ->

                }
                .setNegativeButton("NO") { dialog, which ->

                }
                .setIcon(R.drawable.ic_launcher_foreground)
                .show()

0

ඔබට ක්‍රියාකාරකම් නිර්මාණය කළ හැකි අතර AppCompatActivity දිගු කරයි. ඉන්පසු මැනිෆෙස්ට් හි ඊළඟ ශෛලිය තබන්න:

<activity android:name=".YourCustomDialog"
            android:theme="Theme.AppCompat.Light.Dialog">
</activity>

බොත්තම් සහ පෙළ දර්ශන මගින් එය වැඩි කරන්න

ඉන්පසු මෙය සංවාදයක් මෙන් භාවිතා කරන්න.

උදාහරණයක් ලෙස, රේඛීය සැකැස්ම තුළ මම ඊළඟ පරාමිතීන් පුරවන්නෙමි:

android:layout_width="300dp"
android:layout_height="wrap_content"
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.