如何在android中以编程方式创建alertdialog

时间:2014-08-11 05:17:41

标签: android alertdialog

  

在我的Android应用程序中,我需要以编程方式使用alertdialog,第一行有两个图像,第二行有另外两个图像。我看到了一些例子,

CharSequence colors[] = new CharSequence[] {"red", "green", "blue", "black"};

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Pick a color");
builder.setItems(colors, new DialogInterface.OnClickListener() {
  @Override
public void onClick(DialogInterface dialog, int which) {
    // the user clicked on colors[which]
}
});
builder.show();
  在上面的示例中,

对于每一行只有一个选项,如第一行中的红色,下一行中的绿色,第三行中的蓝色等。我需要在每一行中实施2个选项,请指导我,

1 个答案:

答案 0 :(得分:0)

Dialog mDialog;
mDialog=new Dialog(SplashScreen.this);
            mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            mDialog.setContentView(R.layout.yourcustomlayout);
            TextView ok,cancel;
            ok=(TextView) mDialog.findViewById(R.id.dialogyes);
            cancel=(TextView) mDialog.findViewById(R.id.dialogno);
            ok.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {


                    mDialog.cancel();

                }
            });
            cancel.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    finish();
                    mDialog.cancel();
                }
            });
            mDialog.show();

yourcustomlayout.xml -

<?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"
    android:background="@android:color/white" >

    <TextView
        android:id="@+id/dialogtitle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp"
        android:gravity="center"
        android:text="Internet Connection"
        android:textAppearance="@android:style/TextAppearance.Medium"
        android:textColor="@android:color/black" />

    <View
        android:id="@+id/view1"
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/dialogtitle"
        android:layout_marginTop="25dp"
        android:background="#C83437" />

    <TextView
        android:id="@+id/dialogMessage"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/view1"
        android:layout_marginBottom="20dp"
        android:layout_marginTop="10dp"
        android:gravity="center"
        android:text="You are not connected to internet.\nDo you want to on connection?"
        android:textAppearance="@android:style/TextAppearance.Medium"
        android:textColor="@android:color/black" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="70dp"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="150dp"
        android:gravity="center"
        android:orientation="horizontal"
        android:weightSum="1" >

        <TextView
            android:id="@+id/dialogyes"
            android:layout_width="wrap_content"
            android:layout_height="60dp"
            android:layout_marginBottom="10dp"
            android:layout_weight="0.5"
            android:gravity="center"
            android:text="YES"
            android:textColor="@android:color/black" />

        <TextView
            android:id="@+id/dialogno"
            android:layout_width="wrap_content"
            android:layout_height="60dp"
            android:layout_marginBottom="10dp"
            android:layout_weight="0.5"
            android:gravity="center"
            android:text="NO"
            android:textColor="@android:color/black" />
    </LinearLayout>

</RelativeLayout>

在mDialog.setContentView()中添加自定义布局。