UI基礎控件

AutoCompleteTextView,MultiAutoCompleteTextView,ToggleButton,CheckBox,RadioGroupandroid

public class MainActivity extends AppCompatActivity {

    private String[] arr = {"guangzhou1","guangzhou2","guangzhou3","beijing1","beijing2"};
    
    private AutoCompleteTextView autoCompleteTextView;
    private MultiAutoCompleteTextView multiAutoCompleteTextView;

    private ToggleButton toggleButton;
    private CheckBox checkBox;

    private RadioGroup radioGroup;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        /*
        * 1,初始化控件
        * 2,建立適配器
        * 3,初始化數據源
        * 4,關鍵適配器
        *
        * android.R.layout.simple_list_item_1 指的是一種樣式,ArrayAdapter在這裏和iOS的cell類似
        * */

        autoCompleteTextView = (AutoCompleteTextView)findViewById(R.id.autotext1);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,arr);
        autoCompleteTextView.setAdapter(adapter);

        multiAutoCompleteTextView = (MultiAutoCompleteTextView)findViewById(R.id.mulautotext1);
        multiAutoCompleteTextView.setAdapter(adapter);
        //設置逗號爲分隔符
        multiAutoCompleteTextView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());

        toggleButton = (ToggleButton)findViewById(R.id.toggleButton);
        toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                System.out.println(isChecked?"開":"關");
            }
        });

        checkBox = (CheckBox)findViewById(R.id.checkBox);
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    String str = checkBox.getText().toString();
                    Toast.makeText(MainActivity.this,str,Toast.LENGTH_SHORT).show();
                }
            }
        });

        radioGroup = (RadioGroup)findViewById(R.id.radioGroup);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                String str = "";
                switch (checkedId){
                    case R.id.radioButton:
                        str = "男";
                        break;
                    case R.id.radioButton2:
                        str = "女";
                        break;
                }
                Toast.makeText(MainActivity.this,str,Toast.LENGTH_SHORT).show();
            }
        });
    }


}
相關文章
相關標籤/搜索