我正在尝试为微调器使用自定义行布局,如下所示:
String[] countryArr={"USA","Canada","Other"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.row, R.id.text, countryArr);
spinnerCountry.setAdapter(adapter);
row.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:orientation="horizontal"
android:paddingBottom="10dp"
android:paddingLeft="20dp"
android:paddingTop="10dp" >
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/BLACK"
android:textSize="26dp" />
<RadioButton
android:id="@+id/radio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true" />
</RelativeLayout>
和java代码:
spinnerCountry.setOnItemSelectedListener(new MyOnItemSelectedListener());
public class MyOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos,
long id) {
selected = parent.getItemAtPosition(pos).toString().trim();
Log.i("MyOnItemSelectedListener","selected ="+selected);
}
@SuppressWarnings("rawtypes")
public void onNothingSelected(AdapterView parent) {
etCountry.setText("");
Log.i("MyOnItemSelectedListener","nothing selected");
}
}
当我启动应用程序时,我会在日志中得到此信息:"selected =Other"
因为它是默认的。
但是当我点击微调器时,我得到如下所示的屏幕,onItemSelected
不起作用,因此始终显示警告框,允许选择所有3个值但不执行任何操作。
main.xml中
<com.Mypackage.MySpinner
android:id="@+id/SpinnerCountry1"
android:layout_width="45dp"
android:layout_height="59dp"
android:layout_marginLeft="160dp"
android:background="@drawable/selector_slim_spinner"
android:clickable="true"
android:entries="@array/blankarray" />
public class MySpinner extends Spinner {
private Context _context;
public MySpinner(Context context, AttributeSet attrs) {
super(context, attrs);
_context = context;
}
public MySpinner(Context context) {
super(context);
_context = context;
}
public MySpinner(Context context, AttributeSet attrs, int defStyle) {
super(context);
_context = context;
}
@Override
public View getChildAt(int index) {
View v = new View(_context);
return v;
}
}
请注意如果我没有为微调器使用自定义行布局,OnItemSelected
工作正常。
任何帮助表示感谢。
答案 0 :(得分:3)
将row.xml更改为
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:orientation="horizontal"
android:paddingBottom="10dp"
android:paddingLeft="20dp"
android:paddingTop="10dp" >
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/BLACK"
android:textSize="26dp"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false" />
<RadioButton
android:id="@+id/radio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"/>
</RelativeLayout>
并尝试
答案 1 :(得分:0)
您是否尝试使用自定义ArrayAdapter在RadioButton上设置侦听器?
答案 2 :(得分:0)
我认为,这可能已经完成了这项工作
在onCreate上初始化微调器后。
yourspinner.setOnItemSelectedListener(this);