1. 单选按钮
package org.lxh.demo;
import android.app.Activity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
public class MyRadioListenerDemo extends Activity {
private TextView show = null ;
private RadioGroup sex = null ;// 取得单选钮
private RadioButton male = null ;
private RadioButton female = null ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);
this.show = (TextView) super.findViewById(R.id.show) ;// 取得文本框
this.sex = (RadioGroup) super.findViewById(R.id.sex) ;// 取得单选钮选项
this.male = (RadioButton) super.findViewById(R.id.male) ;
this.female = (RadioButton) super.findViewById(R.id.female) ;
this.sex.setOnCheckedChangeListener(new OnCheckedChangeListenerImpl()) ;
}
private class OnCheckedChangeListenerImpl implements
OnCheckedChangeListener {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
String temp = null ;
if (MyRadioListenerDemo.this.male.getId() == checkedId) {
temp = MyRadioListenerDemo.this.male
.getText().toString(); // 取得单选钮文本
}
if (MyRadioListenerDemo.this.female.getId() == checkedId) {
temp = MyRadioListenerDemo.this.female
.getText().toString();// 取得单选钮文本
}
MyRadioListenerDemo.this.show
.setText("您的性别是:" + temp);// 设置文本显示
}
}
}
2. 下拉列表框 OnItemSelectedListener
private class OnItemSelectedListenerImpl implements OnItemSelectedListener {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view,
int position, long id) {// 选项选中时触发
String value = adapterView.getItemAtPosition(position).toString();// 取得选项内容
MySpinnerListenerDemo.this.info.setText("您喜欢的城市是:" + value);
}
上面的程序中
AdapterView<?> adapterView, java泛型 ?是通配符
3. 焦点事件
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
this.edit.setOnFocusChangeListener(new OnFocusChangeListenerImpl());// 设置焦点事件
this.edit.setOnClickListener(new OnClickListenerImpl()) ;// 设置单击事件
private class OnFocusChangeListenerImpl implements OnFocusChangeListener
4. 长按事件
this.img.setOnLongClickListener(new OnLongClickListenerImpl()); // 定义长按监听
private class OnLongClickListenerImpl implements OnLongClickListener
5. ScrollView
继承于 FrameLayout,作为一个能滚动内容的容器。一般包含一个组件 LinerLayout,再由LinerLayout包含多个组件。
6。 ListView