Spinner和popupwindow都差不多,弹出菜单,AutoCompleteTextView:输入一个字是,可以弹出很多课供选择的内容,
Spinner:监听事件:OnItenSelectListener();
new Spinner(0.setselection(1,true)设置默认选择,不会触发监听,
可以自定义适配器继承自BaseAdapter;如:
public class MySpinnerBaseAdapter extends BaseAdapter { private List<MyGridView> list; private LayoutInflater mInflater; public MySpinnerBaseAdapter(Context context,List<MyGridView> lists) { list = lists; mInflater = LayoutInflater.from(context); } public int getCount() { return list.size(); } public Object getItem(int position) { return list.get(position); } public long getItemId(int position) { return position; } public View getView(int position, View convertView, ViewGroup parent) { Resous resou ; if(convertView == null ){ convertView = mInflater.inflate(R.layout.spinner_two_layout, null); resou = new Resous(); resou.mImage = (ImageView) convertView.findViewById(R.id.simple_image); resou.mtext = (TextView) convertView.findViewById(R.id.spinner_text); convertView.setTag(resou); }else{ resou = (Resous) convertView.getTag(); } MyGridView gridv = (MyGridView) getItem(position); resou.mImage.setBackgroundResource(gridv.getmImage()); resou.mtext.setText(gridv.getmText()); return convertView; } class Resous{ ImageView mImage; TextView mtext; } }
popupwindow:isShowing()是否弹出对话框,dismiss();销毁对话框,
public class PopupWindowActivity extends Activity { private ListView mListPop; private Button mPopBtn; private PopupWindow mPopWind; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_popup_layout); mPopBtn = (Button) findViewById(R.id.popupwindow_btn); // 实例化窗口,设置弹窗的大小 mPopWind = new PopupWindow(getView(), 400, 400); // 下面这三行代码可以让你点击空白处来关闭PopWindow 弹出的窗口 mPopWind.setTouchable(true); mPopWind.setOutsideTouchable(true); mPopWind.setBackgroundDrawable(new BitmapDrawable()); // 点击按钮就弹出窗口 mPopBtn.setOnClickListener(new OnClickListener() { public void onClick(View v) { if (mPopWind.isShowing()) {// 判断窗口是否已经弹出弹出 // mPopWind.setOutsideTouchable(false); mPopWind.dismiss();// 关闭窗口 } else { mPopWind.showAsDropDown(v);// 弹出窗口 } } }); }
AutoCompleteTextView :和EditText输入框一样的属性,特有的一个属性setThreshold(1);设置输入一个字符就可以联想,默认的是2个字符.
public class AutocompleteTextViewActivity extends Activity { private AutoCompleteTextView mAutoText; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.autocomplete_layout); mAutoText = (AutoCompleteTextView) findViewById(R.id.auto_Text); mAutoText.addTextChangedListener(new TextWatcher() { public void onTextChanged(CharSequence s, int start, int before, int count) { String sss = mAutoText.getText().toString(); if (sss.equals("m")) { String[] s2 = { "mmm", "mss", "wsss", "ms" }; ArrayAdapter<String> adapter = new ArrayAdapter<String>( AutocompleteTextViewActivity.this, android.R.layout.simple_expandable_list_item_1, s2); mAutoText.setAdapter(adapter); } else if (sss.equals("i")) { String[] s2 = { "iphone", "ipmss", "iwsss", "ims" }; ArrayAdapter<String> adapter = new ArrayAdapter<String>( AutocompleteTextViewActivity.this, android.R.layout.simple_expandable_list_item_1, s2); mAutoText.setAdapter(adapter); } } public void beforeTextChanged(CharSequence s, int start, int count, int after) { } public void afterTextChanged(Editable s) { } }); } }