setContentView,android中的微调器自定义alertdialog

时间:2016-09-16 12:21:39

标签: android spinner alertdialog setcontentview

我想用Spinner创建一个自定义alertdialog。所以我使用了以下代码。

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

    alertDialog.setTitle("Thêm khoản thu");
    //final LayoutInflater layoutInflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
   // final  View dialogView = layoutInflater.inflate(R.layout.thu_thong_bao_them, null);//Tạo dialogview từ layout thông báo
    View dialogView = getLayoutInflater().inflate(R.layout.thu_thong_bao_them, null);
    spinner=(Spinner)dialogView.findViewById(R.id.spdanhmuc_them);

    MyDatabaseHelper db = new MyDatabaseHelper(getApplicationContext());

    List<String> lables = db.Load_danhmucthu();

    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, lables);

    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    spinner.setAdapter(dataAdapter);

    alertDialog.setView(dialogView);// đặt dialogview vào alertdialog
    alertDialog.setIcon(R.drawable.add48x48);//đặt icon

    alertDialog.setPositiveButton("Thêm",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {


                    }

            });

    alertDialog.setNegativeButton("Hủy",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });

    alertDialog.show();

DialogAlert thu_thong_bao_them.xml的布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:padding="20dp">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Danh mục"
        android:id="@+id/tvdanhmuc_them"/>
    <Spinner
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/tvdanhmuc_them"
        android:layout_marginLeft="20dp"
        android:id="@+id/spdanhmuc_them"
        android:spinnerMode="dropdown">
    </Spinner>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Nội dung"
        android:id="@+id/tvnoidung_them"
        android:layout_below="@+id/tvdanhmuc_them"
        android:layout_marginTop="20dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"/>
    <EditText
        android:id="@+id/edtnoidung_them"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/tvnoidung_them"
        android:layout_below="@id/spdanhmuc_them"
        android:layout_marginLeft="20dp"
        android:inputType="text" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Ngày thu"
        android:id="@+id/tvngaythu_them"
        android:layout_below="@+id/tvnoidung_them"
        android:layout_marginTop="20dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"/>
    <EditText
        android:id="@+id/edtngaythu_them"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:inputType="text"
        android:layout_toRightOf="@id/tvnoidung_them"
        android:layout_below="@id/edtnoidung_them"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Số tiền"
        android:id="@+id/tvsotien_them"
        android:layout_marginTop="20dp"
        android:layout_below="@+id/tvngaythu_them"

        />
    <EditText
        android:id="@+id/edtsotien_them"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/tvsotien_them"
        android:layout_below="@id/edtngaythu_them"
        android:layout_marginLeft="30dp"
        android:inputType="text" />
</RelativeLayout>

Image Befor I call AlertDialog

Image After call alertdialog (data has been load into spinner but main_layout was replaced by layout of alertdialog and spinner in Dialog hasn't data)

请帮帮我。

1 个答案:

答案 0 :(得分:0)

创建自定义视图后,将其设置为警告对话框视图,不要设置与图标,正按钮等对话框相关的任何内容。