如何在Android的编程方式创建alertdialog

如何在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();
解决方案
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>

in mDialog.setContentView() add your custom layout.

这篇关于如何在Android的编程方式创建alertdialog的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 19:30