spinner setOnItemSelectedListener在自定义对话框类中不起作用

时间:2016-08-18 09:58:27

标签: java spinner

我遇到一个问题,即微调器的setOnItemSelectedListener在自定义对话框类中不起作用。它实际上返回微调器视图也ok按钮工作正常!

public class Custom_dialog extends Dialog implements
    android.view.View.OnClickListener {
public String[] arraySpinner;
public Activity c;
public Dialog d;
Spinner s;
public Button ok, annuler;
   public Custom_dialog(Activity a) {
    super(a);
    // TODO Auto-generated constructor stub
    this.c = a;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.doc_type_dialog);
    this.arraySpinner = new String[] {
            "1", "2", "3", "4", "5"
    };
     this.s = (Spinner) findViewById(R.id.dropdown);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(),
            android.R.layout.simple_spinner_item, arraySpinner);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    this.s.setAdapter(adapter);
    ok = (Button) findViewById(R.id.ok_doc_type);
    annuler = (Button) findViewById(R.id.annuler_doc_type);
    ok.setOnClickListener(this);
    annuler.setOnClickListener(this);
}
@Override
public void onClick(final View v) {


    //System.out.println("********"+v.getId()+"*********");
    switch (v.getId()) {
        case R.id.ok_doc_type:

            this.s.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

                @Override
                public void onItemSelected(AdapterView<?> arg0, View arg1,
                                           int pos, long arg3) {

                    String selected = arg0.getItemAtPosition(pos).toString();
                    Toast.makeText(getContext(), "item spinner click"+selected, Toast.LENGTH_SHORT).show();
                    System.out.println("********"+selected+"*********");
                    //Intent intent = new Intent(getContext(), RechercheDocument.class);
                   // c.getParent().startActivity(intent);

                }

                @Override
                public void onNothingSelected(AdapterView<?> arg0) {
                    // TODO Auto-generated method stub

                }
            });
            break;
        case R.id.annuler_doc_type:
            dismiss();
            break;
        default:
            break;
    }
    dismiss();
}
}

这是我的微调器的XML布局文件:

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="250dp"

        android:background="@drawable/background_popup"
        android:orientation="vertical"
        android:gravity="center_vertical|center_horizontal"
        android:layout_height="wrap_content">


        <TextView
            android:layout_width="239dp"
            android:layout_height="70dp"
            android:layout_marginTop="7dp"
            android:layout_gravity="center"
            android:background="@drawable/bg_lib_article"
            android:text="Type document"
            android:gravity="center"

            android:textColor="#5DBCD2"
            android:textSize="15dp"
            android:textStyle="bold"
            android:id="@+id/lib_article" />


    <Spinner
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/dropdown"
        android:layout_gravity="center_horizontal" />

    <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:background="#3E80B4"
            android:orientation="horizontal"
            android:layout_marginTop="15dp"
            android:weightSum="1">

            <Button
                android:id="@+id/annuler_doc_type"
                android:layout_width="121dp"
                android:layout_height="45dp"
                android:background="@drawable/bg_btn_annuler"
                android:clickable="true"
                android:text="Annuler"
                android:textColor="#5DBCD2"
                android:textStyle="bold"
                android:layout_marginRight="1dp"
                android:layout_marginBottom="1dp" />

            <Button
                android:id="@+id/ok_doc_type"
                android:layout_width="117dp"
                android:layout_height="45dp"
                android:background="@drawable/bg_btn_annuler"
                android:clickable="true"
                android:text="Ok"
                android:textColor="#5DBCD2"
                android:textStyle="bold"
                android:layout_marginLeft="1dp"

                android:layout_marginBottom="1dp" />
        </LinearLayout>

    </LinearLayout>

0 个答案:

没有答案