2016-09-16 10 views
0

Spinnerでカスタムアラートダイアログを作成したいのですが、次のコードを使用しました。 DialogAlertためsetContentView、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(); 

レイアウトは、私を助けてください

​​

Image Befor I call AlertDialog

<?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> 

をthu_thong_bao_them.xml。

答えて

0

後にウル答えのため、正のボタンなど

+0

感謝をアイコンのようなダイアログに関連したものを設定していない、ダイアログビューに警告するためにそれを設定し、カスタムビューを作成します。アイコン、posボタンを削除した後。 alertdialogが表示しましたが、スピナーにデータがありませんでしたか?そして、私はいくつかの機能を行うためにボタンを追加することはできますか?OK、Cancle、..? – Chien

+0

すでにアラートダイアログ用のカスタムレイアウトを作成しています。必要に応じてそのレイアウトにボタンを追加することができます。 –

+0

ありがとうございました:)) – Chien

関連する問題