高级控件1

高级控件之
AutoCompleteTextView 自动提示

AutoCompleteTextView 属性有;

		android:layout_width="match_parent" 控件的高与宽
		android:layout_height="wrap_content"
		
		android:completionThreshold="1" //设置输入产生提醒字符的字符数量
				高级控件之
				ListVeiw 用来展示列表的View 属性有: 
				
		android:id="@+id/lv_list_names"//id属性 
		高级控件之
		
		Spinner //下拉框控件
		

代码演示

<AutoCompleteTextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
      android:id="@+id/actv_main_auto"
      android:completionThreshold="1"//
设置输入产生提醒字符的字符数量
/> < MultiAutoCompleteTextView android :layout_width= "match_parent" android :layout_height= "wrap_content" android :id= "@+id/mactv_main_auto" android :completionThreshold= "1" /> < Spinner android :layout_width= "match_parent" android :layout_height= "wrap_content" android :id= "@+id/s_main_spinner" > Spinner>
			
public class MainActivity extends AppCompatActivity {

    private ListView lv_list_names;
    private Spinner s_main_spinner;
    private String[] data;
    private MultiAutoCompleteTextView mactv_main_auto;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获取控件
        mactv_main_auto = (MultiAutoCompleteTextView) findViewById(R.id.mactv_main_auto);
        lv_list_names = (ListView) findViewById(R.id.lv_list_names);
        s_main_spinner = (Spinner) findViewById(R.id.s_main_spinner);
        //获取字符串数组
        data = getResources().getStringArray(R.array.data);
        //适配器
        ArrayAdapter adapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1, data);
        mactv_main_auto.setAdapter(adapter);
        //给控件设分隔符
        MultiAutoCompleteTextView.Tokenizer tokenizer=new MultiAutoCompleteTextView.CommaTokenizer();
        mactv_main_auto.setTokenizer(tokenizer);
        mactv_main_auto.setAdapter(adapter);
        //给下拉框设置适配器
        final String provinces[]={"湖南省","河南省","海南省"};
        int images[]={R.drawable.dog,R.drawable.elephant,R.drawable.fox};
        List> list=new ArrayList<>();
        for (int i = 0; i < provinces.length; i++) {
            Map map=new HashMap<>();
            map.put("title",provinces[i]);
            map.put("image",images[i]);
            list.add(map);
        }
        SimpleAdapter adapterSpinner=new SimpleAdapter(this,list,android.R.layout.activity_list_item,new String[]{"title","image"},new int[]{android.R.id.text1,android.R.id.icon});
        s_main_spinner.setAdapter(adapterSpinner);
        s_main_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView adapterView, View view, int i, long l) {
                Toast.makeText(MainActivity.this, provinces[i], Toast.LENGTH_SHORT).show();
            }
            @Override
            public void onNothingSelected(AdapterView adapterView) {

            }
        });
    }


你可能感兴趣的:(高级控件1)