Android总结(三)

来源:互联网 发布:胡歌女朋友知乎 编辑:程序博客网 时间:2024/06/06 00:56

事件处理
1.所有的事件产生之后,将自动调用对应的事件处理方式,如果已经存在事件的监听操作,则使用指定的事件处理方式,通过事件监听器对事件进行处理;如果没有相应的处理程序,则放弃该事件。
单击事件
1.使用按钮触发一些事件,可以通过单击事件完成。单击事件使用View.OnClickListener接口进行事件的处理。此接口定义为:
public static interface View.OnClickListener{
public void onClick(View v); }
要想设置单击事件操作接口,直接使用setOnClickListener()方法即可,当事件触发之后,使用onClick()方法执行具体的处理操作。
OnClickListener是使用static声明的内部接口,属于内部静态接口,此接口相当于一个外部接口。
2.改变屏幕显示方向:
setRequestedOrientation(int requestedOrientation)方法:设置显示方向;;
getRequestedOrientation()方法:取得当前屏幕方向
onConfigurationChanged(Configuration newConfig)方法:系统设置改变时触发此事件,表示对系统设置的改变进行监听。
public class MyClickDemo extends Activity {
private Button change = null;
private ImageView img = null;

@Overridepublic void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    super.setContentView(R.layout.main);    this.change = (Button) super.findViewById(R.id.change); // 取得按钮    this.img = (ImageView) super.findViewById(R.id.img); // 取得图片    this.change.setOnClickListener(new MyOnClickListenerImpl()); // 设置监听操作}private class MyOnClickListenerImpl implements OnClickListener { // 单击事件    public void onClick(View v) {        if (MyClickDemo.this.getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {// 无法进行画面的旋转            MyClickDemo.this.change.setText("错误:无法改变屏幕方向。");        } else {            if (MyClickDemo.this.getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) { // 现在的方向是横屏显示                MyClickDemo.this                        .setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); // 变为竖屏显示            } else if (MyClickDemo.this.getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) { // 如果为竖屏显示                MyClickDemo.this                        .setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); // 变为横屏显示            }        }    }}@Overridepublic void onConfigurationChanged(Configuration newConfig) { // 表示的是系统设置修改的时候触发    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) { // 现在的屏幕方向是横屏        MyClickDemo.this.change.setText("改变屏幕方向为竖屏显示(当前为横屏显示)");        MyClickDemo.this.img.setImageResource(R.drawable.mldn_landscape);// 显示横屏图片    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) { // 现在竖屏        MyClickDemo.this.change.setText("改变屏幕方向为竖屏显示(当前为横屏显示)");        MyClickDemo.this.img.setImageResource(R.drawable.mldn_portrait);// 显示竖屏图片    }    super.onConfigurationChanged(newConfig);}

}

当用户使用setRequestedOrientation()方法修改了屏幕显示方向之后,会触发onConfigurationChanged()事件。
权限配置:
(1)在activity中:
android:screenOrientation=”portrait”//默认显示方向为竖屏
android:configChanges=”orientation|keyboard”//配置configChanges事件
(2)再添加://设置允许改变屏幕信息的权限

(3)如果想让屏幕自适应方向:只需在activity中用
android:screenOrientation=”sensor”//自适应屏幕
3.明文显示密码:
密文显示:android.text.method.HideReturnsTransformationMethod
明文显示:android.text.method.PasswordTransformationMethod
在使用这两个子类时,直接利用这两个类提供的getInstance()方法即可。
public class MyClickDemo extends Activity {
private EditText password = null;
private CheckBox show = null;

@Overridepublic void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    super.setContentView(R.layout.main);    this.password = (EditText) super.findViewById(R.id.password);    this.show = (CheckBox) super.findViewById(R.id.show);    this.show.setOnClickListener(new OnClickListenerImpl());}private class OnClickListenerImpl implements OnClickListener {    public void onClick(View v) {        if (MyClickDemo.this.show.isChecked()) { // 被选中,应该采用明文的方式显示            MyClickDemo.this.password                    .setTransformationMethod(HideReturnsTransformationMethod                            .getInstance()); // 将文本框的内容设置为明文显示        } else { // 采用密文的方式显示            MyClickDemo.this.password                    .setTransformationMethod(PasswordTransformationMethod                            .getInstance()); // 以密文的方式显示        }    }}

}
单选按钮与OnCheckedChangeListener
1.注册事件的方法为:public void setOnCheckedChangeListener(RadioGroup.OnCheckedChangeListener listener).
2.
public class MyRadioListenerDemo extends Activity {
private TextView show = null;
private RadioGroup sex = null;
private RadioButton male = null;
private RadioButton female = null;

@Overridepublic 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 {    public void onCheckedChanged(RadioGroup group, int checkedId) {        String temp = null; // 保存以后show组件要显示的文本信息        if (MyRadioListenerDemo.this.male.getId() == checkedId) { // 现在选中的ID和组件的ID一致            temp = MyRadioListenerDemo.this.male.getText().toString(); // 取得信息        }        if (MyRadioListenerDemo.this.female.getId() == checkedId) { // 现在选中的ID和组件的ID一致            temp = MyRadioListenerDemo.this.female.getText().toString(); // 取得信息        }        MyRadioListenerDemo.this.show.setText("您的性别是:" + temp);// 设置文本组件的内容    }}

}
下拉列表框与OnItemSelectedListener
1.联动菜单——就是指提供两个下拉列表,当地一个下拉列表的选项发生改变时,第二个下拉列表也可以显示出与一级下拉列表相关的数据项。
(1)定义城市信息的资源文件—-values\city_data.xml:

0 0
原创粉丝点击