• Android 基本控件使用


    final TextView tx = (TextView)findViewById(R.id.textView1);

    // 自动补齐的TextView

    AutoCompleteTextView mPlace = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);

    String[] books = new String[] { "rollen", "allen", "rollenren", "roll" };

    // 先定义一个原始数据的适配器

    ArrayAdapter adapter =new ArrayAdapter<String>

        (MainActivity.this,android.R.layout.simple_dropdown_item_1line,books);

    // 设定TextView的适配器

    mPlace.setAdapter(adapter); 

    // 输入多个自动补齐的TextView

    MultiAutoCompleteTextView mtexts =    (MultiAutoCompleteTextView)findViewById(R.id.multiAutoCompleteTextView1);

    // 设定TextView的适配器

    mtexts.setAdapter(new ArrayAdapter<String>

    (MainActivity.this, android.R.layout.simple_dropdown_item_1line,books));

    // 设定适配器的 分割符,如逗号

    mtexts.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());

    // 限定输入的字符的长度

    final EditText text_filterred = (EditText)findViewById(R.id.editText1);

    text_filterred.setFilters(new InputFilter[]{

        new InputFilter.AllCaps(),

        new InputFilter.LengthFilter(2)

    });

    // Button按钮实现Activity跳转传递参数

    final Button btn = (Button)findViewById(R.id.button1);

    btn.setOnClickListener(new OnClickListener(){

        @Override

        public void onClick(View v) {

            Intent intent = new Intent();

            intent.setClass(MainActivity.this, Test1Activity.class);

            intent.putExtra("str", "come from first activity");

            startActivity(intent);

        }

    });

     

    // CheckBox 的设定及响应事件

    final CheckBox check_button = (CheckBox) findViewById(R.id.checkBox1);

    check_button.setOnClickListener(new OnClickListener(){

        @Override

        public void onClick(View v) {

            tx.setText(check_button.isChecked()?"选中了":"没选中");

        }

    });

     

    // ToggleButton的设定及响应事件

    final ToggleButton toggleButton = (ToggleButton)findViewById(R.id.toggleButton1);

    toggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener(){

        @Override

        public void onCheckedChanged(CompoundButton buttonView,boolean isChecked)    {

            tx.setText(isChecked?"选中的":"未选中");

        }

     });

     

    // 单选按钮RadioGroup的设定及响应事件

    final RadioGroup group = (RadioGroup)findViewById(R.id.radioGroup1);

    // 清除选择

    group.clearCheck();

    group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){

        @Override

        public void onCheckedChanged(RadioGroup group, int checkedId) {

            if(checkedId!=-1){

                RadioButton rb =(RadioButton) findViewById(checkedId);

                if(rb!=null){

                    tx.setText("You chose:"+rb.getText());

                }

            }

        }          

    });

     

    // 下拉列表Spinner的设定及响应事件

    <Spinner

        android:id="@+id/spinner1"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:entries="@array/colors"

        android:prompt="@string/spin_prompt" />

    利用array 里面的内容指定Spinner 的数据源

    spin = (Spinner) findViewById(R.id.spinner1);

    spin.setOnItemSelectedListener(new OnItemSelectedListener(){

        @Override

        public void onItemSelected(AdapterView<?> arg0, View arg1,

            int arg2, long arg3) {

            TextView tvn=(TextView)arg1;//获取其中的TextView

            if(tvn!=null){

                tx.setText(tvn.getText().toString());

            }

        }

     

        @Override

        public void onNothingSelected(AdapterView<?> arg0) {

            // TODO Auto-generated method stub

                   

        }

    });

  • 相关阅读:
    326周日去找书
    新视野大学英语-Book1
    预编译头文件来自编译器的早期版本,或者预编译头为 C++ 而在 C 中使用它(或相反)
    自定义GRUB主题
    Linux安装CMake
    Linux编译安装Apache
    @Scheduled注解
    熵权可拓物元模型
    Linux更新Python3.8
    Linux下更新GCC
  • 原文地址:https://www.cnblogs.com/oftenlin/p/2918474.html
Copyright © 2020-2023  润新知