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
}
});