How to create custom alert dialog in android?

2019-01-23 04:02发布

I am developing a sample app.I am able to show alert on button click having some tittle and button .But now I want to show a pop up window having username (Label) and text field (Edit field) and a button. on click on button.can I make another popup xml file for that ?

public void selfDestruct(View view) {
         // Kabloey
         Log.d("Naveen", "Test====");
         System.out.println("----------------------ghfgjhf-----------------");
         AlertDialog alertDialog = new AlertDialog.Builder(SecondActivity.this).create();
         alertDialog.setTitle("Reset...");
         alertDialog.setMessage("R u sure?");
         alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int which) {

               //here you can add functions

            } });
         alertDialog.show();
     }

 

   <Button
     android:layout_height="wrap_content"
     android:layout_width="wrap_content"
     android:text="@string/self_destruct"
     android:onClick="selfDestruct" />

9条回答
贼婆χ
3楼-- · 2019-01-23 04:05

Use the below coding to display a Pop up in android.

AlertDialog.builder builder=new AlertDilaog.Builder(this);
    builder.setMessage("PopUP Example");
    AlertDialog alert=builder.create();
    alert.setTitle("");
    alert.show();

    new Handler.postDelayed(new Runnable()
    {
    @override
    public void run()
    {
  //TODO
    alert.dismiss();
    }
    },1*1000);
    }

Youtube link: https://www.youtube.com/watch?v=SEsVTTl6exg

查看更多
女痞
4楼-- · 2019-01-23 04:07

Try this...

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Reset...").setView(editText)
   .setMessage("R u sure?").setCancelable(true)
   .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int arg1) {
        //Do here whatever.....
    }
});
AlertDialog alert = builder.create();
alert.show();
}
查看更多
冷血范
5楼-- · 2019-01-23 04:08

you can use Dialog , like this code :

final Dialog dialog = new Dialog(context);
// dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.custom);
dialog.setTitle("Title...");

// set the custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Android custom dialog example!");
Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();

if you want to remove title bar just use this code after define :

dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
查看更多
爱情/是我丢掉的垃圾
6楼-- · 2019-01-23 04:09

custom_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >


    <EditText
        android:id="@+id/editText"
        android:hint="Enter some thing"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

    <requestFocus />
    </EditText>

    <LinearLayout                
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"   
        android:layout_marginTop="10dp">     

        <Button
            android:id="@+id/save"
            android:layout_marginTop="15dp"
            android:layout_weight="1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="SAVE" />

        <Button
            android:id="@+id/cancel"
            android:layout_marginTop="15dp"
            android:layout_weight="1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Cancel" />

    </LinearLayout>
</LinearLayout>

Need to inflate custom_dialog.xml

final Dialog dialog = new Dialog(this);

dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("Custom Alert Dialog");

final EditText editText = (EditText) dialog.findViewById(R.id.editText);
Button btnSave          = (Button) dialog.findViewById(R.id.save);
Button btnCancel        = (Button) dialog.findViewById(R.id.cancel);
dialog.show();
查看更多
再贱就再见
7楼-- · 2019-01-23 04:14

its working for me

Dialog m_dialog; 
m_dialog = new Dialog(BusinessDetail.this);
            m_dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

            LayoutInflater m_inflater = LayoutInflater.from(BusinessDetail.this);
            View m_view = m_inflater.inflate(R.layout.rateus_popup, null);
            myPopLay = (LinearLayout) m_view.findViewById(R.id.myPopLay);
m_dialog.setContentView(m_view);
            m_dialog.show();
查看更多
登录 后发表回答