美文网首页
Android-Spinner简单使用

Android-Spinner简单使用

作者: 见哥哥长高了 | 来源:发表于2019-07-10 10:56 被阅读0次

    我们要实现的一个效果是:菜单展示,菜单中数据的添加与删除操作。
    具体用法与逻辑如下:

    main.xml文件中加入两个按钮,一个文本输入框

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/button_01"
                android:text="添加一个"/>
    
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/button_02"
                android:text="删除一个"/>
    
            <Spinner
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/my_spinner"/>
    

    另外创建两个xml布局文件,分别为:item.xml和drop_down_item, 设置Root elments为TextView用来显示菜单中的item。

    MainActivity.class类中:

    public class MainActivity extends AppCompatActivity {
    
    
        private TextView textview;
        private EditText editText;
        private Button addButton;
        private Button deletButton;
        private Spinner spinner;
        private static final String[] countiersStr = {
                "张三",
                "李四",
                "王五",
                "赵六"
        };
        private ArrayAdapter<String> adapter;
        private List<String> allCountries;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            allCountries = new ArrayList<String>();
    
            for (int i = 0; i < countiersStr.length;i++ ){
    
                allCountries.add(countiersStr[i]);
            }
    
            adapter = new ArrayAdapter<String>(this,R.layout.item,allCountries);
            adapter.setDropDownViewResource(R.layout.drop_down_item);
    
            addButton = (Button)findViewById(R.id.button_01);
            deletButton = (Button)findViewById(R.id.button_02);
            spinner = (Spinner)findViewById(R.id.my_spinner);
            textview = (TextView)findViewById(R.id.text_view01);
            editText = (EditText) findViewById(R.id.edit_text);
    
            spinner.setAdapter(adapter);
    
            addButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
    
                    String newCountry = editText.getText().toString();
    
                    for (int i = 0; i < adapter.getCount();i++){
                        if (newCountry.equals(adapter.getItem(i))){
                            return;
                        }
                    }
    
                    if (!newCountry.equals("")){
    
                        adapter.add(newCountry);
    
                        int position = adapter.getPosition(newCountry);
    
                        spinner.setSelection(position);
    
                        editText.setText("");
                    }
                }
            });
    
    
            deletButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
    
                    //删除值
                    adapter.remove(spinner.getSelectedItem().toString());
    
                    //清除EditText中的内容
                    editText.setText("");
    
                    if (adapter.getCount() == 0){
                        //清除TextView中的内容
                        textview.setText("");
                    }
                }
            });
    
    
            spinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){
    
                public void onItemSelected(AdapterView<?> var1, View var2, int var3, long var4){
    
                    textview.setText(var1.getSelectedItem().toString());
                }
    
                public void onNothingSelected(AdapterView<?> var1){
    
                }
            });
        }
    }
    
    

    相关文章

      网友评论

          本文标题:Android-Spinner简单使用

          本文链接:https://www.haomeiwen.com/subject/nxuckctx.html