2016-09-16 143 views
0

我想用微調創建一個自定義alertdialog。所以我用下面的代碼。setContentView,微調框自定義alertdialog在android

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

​​

請幫助我。

回答

0

後創建自定義視圖,將其設置爲提醒對話框觀點,不設置相關的對話一樣的圖標,正面按鈕等

+0

感謝UR答什麼。我刪除圖標後,pos按鈕; alertdialog已顯示,但微調沒有數據?如何添加按鈕來完成一些功能,例如OK,Cancle,..? – Chien

+0

已經爲警報對話框創建了自定義佈局。您可以根據需要在該佈局中添加按鈕。 –

+0

感謝您的幫助:)) – Chien