android 的spinner是一个什么作用的控件
展开全部
可以使用Button加ListPopupWindow实现和spinner一样的效果,并且可以监听到点击事件(Spinner是不能设置点击事件的)
布局文件,只有一个button
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<Button
android:id="@+id/button"
android:layout_width="100dp"
android:layout_height="50dp"
/>
</LinearLayout>
代码
public class MainActivity extends Activity {
private Button button;
private ListPopupWindow popupWindow;
private List<String> strings;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
strings = new ArrayList<String>();
strings.add("item1");
strings.add("item2");
strings.add("item3");
button = (Button)findViewById(R.id.button);
popupWindow = new ListPopupWindow(this);
popupWindow.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,strings));
popupWindow.setAnchorView(button);
popupWindow.setWidth(LayoutParams.WRAP_CONTENT);
popupWindow.setHeight(LayoutParams.WRAP_CONTENT);
popupWindow.setModal(true);
popupWindow.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
button.setText(strings.get(position));
popupWindow.dismiss();
}
});
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
popupWindow.show();
}
});
}
}
布局文件,只有一个button
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<Button
android:id="@+id/button"
android:layout_width="100dp"
android:layout_height="50dp"
/>
</LinearLayout>
代码
public class MainActivity extends Activity {
private Button button;
private ListPopupWindow popupWindow;
private List<String> strings;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
strings = new ArrayList<String>();
strings.add("item1");
strings.add("item2");
strings.add("item3");
button = (Button)findViewById(R.id.button);
popupWindow = new ListPopupWindow(this);
popupWindow.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,strings));
popupWindow.setAnchorView(button);
popupWindow.setWidth(LayoutParams.WRAP_CONTENT);
popupWindow.setHeight(LayoutParams.WRAP_CONTENT);
popupWindow.setModal(true);
popupWindow.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
button.setText(strings.get(position));
popupWindow.dismiss();
}
});
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
popupWindow.show();
}
});
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询