`

Android学习07-----事件处理(1)单击事件_改变屏幕方向和密码明文显示

阅读更多

 

在前面总结了简单的 Android 控件 Android 中常用的布局 ,那么我们知道要想让我们在 Android 上开发的图形界面有意义,真正的实现那种人机交互的操作,事件的处理是必须的。所以这篇我们总结一下 Android 中的事件处理。

基本上每个组件都会存在相应的事件处理操作,但是其基本的操作流程都是一样的。事件处理:

 

 

 

具体的事件方法大家可以查看 android.view 下的 View 类下,下面我们以一些常用的事件处理来进行说明,考虑到页面显示问题,所以会将几个事件处理拆成几篇博客处理。

一、单击事件

1 、改变屏幕方向 Demo

如果手机的屏幕发生改变了,则肯定属于系统的设置发生了改变,所以一旦发生了系统的设置改变会自动执行 onConfigurationChanged() 方法。

onclick.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    
    <Button 
        android:id="@+id/btn_change"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="改变屏幕方向为横屏显示(当前为竖屏显示)"/>
	
</LinearLayout>

 

 

Activity

package com.iflytek.activity;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class EventActivity extends Activity {

	private Button btnChange = null;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// setContentView(R.layout.main);

		// 1、单击事件处理Demo
		super.setContentView(R.layout.onclick);
		this.btnChange = (Button) super.findViewById(R.id.btn_change);
		this.btnChange.setOnClickListener(new MyOnClickLlistenerImpl());

	}

	private class MyOnClickLlistenerImpl implements OnClickListener {

		@Override
		public void onClick(View v) {
			int screen_dir = EventActivity.this.getRequestedOrientation();// 获取当前屏幕方向,0表示横屏,1表示竖屏
			if (screen_dir == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {// 无法进行画面旋转
				EventActivity.this.btnChange.setText("错误:无法改变屏幕的方向");
			} else {
				if (screen_dir == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {// 现在方向是横屏
					EventActivity.this
							.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);// 变成竖屏
				} else if (screen_dir == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
					EventActivity.this
							.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
				}
			}

		}
	}
	
	/**
	 * 表示的时系统设置修改的时候触发
	 */
	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		//找到当前屏幕方向的一个标记性的常量,通过这个常量把值返回去
		int screen_dir = newConfig.orientation;
		//注意这里不要再使用ActivityInfo.SCREEN_ORIENTATION_PORTRAIT了,因为有时这两个值是不一样的
		if (screen_dir == Configuration.ORIENTATION_LANDSCAPE) {
			EventActivity.this.btnChange.setText("改变屏幕方向为竖屏显示(当前为横屏显示)");
		}else if(screen_dir == Configuration.ORIENTATION_PORTRAIT){
			EventActivity.this.btnChange.setText("改变屏幕方向为横屏显示(当前为竖屏显示)");
		}
		super.onConfigurationChanged(newConfig);
	}

}

 

 

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.iflytek.activity"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".EventActivity"
            android:screenOrientation="portrait" 
            android:configChanges="orientation|keyboard" >
            <!-- android:screenOrientation设置开始按竖屏显示,这里不设置,也改变不了 -->
            <!-- android:configChanges配置configChanges事件 -->
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    
    <!-- 设置允许改变配置信息的权限 -->
    <uses-permission android:name="android.permission.CHANGE_CONFIGURATION"/>

</manifest>

 

2 、明文显示密码 Demo

明文显示: android.text.method.HideReturnsTransformationMethod

密文显示: android.text.method.PasswordTransformationMethod

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/msg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="请输入密码" />
    
    <EditText
        android:id="@+id/password"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:password="true"/>
    
    <CheckBox
        android:id="@+id/show"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="false"
        android:text="显示密码"/>
    

</LinearLayout>

 

Activity:

package com.iflytek.activity;

import android.app.Activity;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.EditText;

public class EventActivity extends Activity {

	private EditText password = null;
	private CheckBox show = null;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		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 {

		@Override
		public void onClick(View v) {
			if (EventActivity.this.show.isChecked()) {// 被选中,则显示明文
				// 将文本框的内容设置成明文显示
				EventActivity.this.password
						.setTransformationMethod(HideReturnsTransformationMethod
								.getInstance());
			} else {
				// 将文本框内容设置成密文的方式显示
				EventActivity.this.password
						.setTransformationMethod(PasswordTransformationMethod
								.getInstance());
			}
		}

	}

}

 

 

 

 

 

 

 

 

  • 大小: 30.8 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics