Android学习笔记(六)响应系统设置事件

configuration类

Configuration类专门用于描述手机设备上的配置信息,这些配置信息即包括用户特定的配置项,也包括系统的动态设备配置。程序可调用Activity的如下方法来获取系统的Configuration对象

Configuration cfg=getResources().getConfiguration();

实例代码

1)MainActivity类

public class MainActivity extends ActionBarActivity {
	// 获取系统的Configuration对象
	// 调用Configuration对象的属性来获取设备的状态

	EditText ori;
	EditText navigation;
	EditText touch;
	EditText mnc;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		ori = (EditText) findViewById(R.id.ori);
		navigation = (EditText) findViewById(R.id.navigation);
		touch = (EditText) findViewById(R.id.touch);
		mnc = (EditText) findViewById(R.id.mnc);
		Button bn = (Button) findViewById(R.id.bn);
		bn.setOnClickListener(new OnClickListener() {
			// 为按钮注册事件监听器
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// 获取系统的configuration对象
				Configuration cfg = getResources().getConfiguration();
				String screen = cfg.orientation == Configuration.ORIENTATION_LANDSCAPE ? "横向屏幕"
						: "竖向屏幕";
				//MNC为网络id,由2位数字组成,
		        //用于识别移动客户所归属的移动网络,中国移动为00,中国联通为01,中国电信为03;
				String mncCode = cfg.mnc + " ";
				String naviName = cfg.orientation == Configuration.NAVIGATION_NONAV ? "没有方向控制"
						: cfg.orientation == Configuration.NAVIGATION_WHEEL ? "滚轮控制方向"
								: cfg.orientation == Configuration.NAVIGATION_DPAD ? "方向键控制方向"
										: "轨迹球控制方向";
				navigation.setText(naviName);
				String touchName=cfg.touchscreen==Configuration.TOUCHSCREEN_NOTOUCH?"无法触摸":"支持触摸屏";
				ori.setText(screen);
				mnc.setText(mncCode);
				touch.setText(touchName);
			}

		});
	}
}

2)布局xml文件配置

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
<EditText 
    android:id="@+id/ori"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="text"
    />
<EditText 
    android:id="@+id/navigation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="text"
    />
<EditText 
    android:id="@+id/touch"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="text"
    />
<EditText 
    android:id="@+id/mnc"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="text"
    />
<Button
    android:id="@+id/bn"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="click me"
    />
</LinearLayout>

3)运行结果

重写onConfigurationChanged响应系统设置更改

如果程序需要监听系统设置的更改,则可以考虑重写Activity的onConfigurationChanged(Configuration newConfig)方法,该方法是一个基于回调的事件处理方法,当系统设置发生更改,该方法会被自动触发。

实例

当屏幕方向发生改变时,修改EditText中的值,并弹出提示框显示屏幕方向修改

1)在上述MainActivity中添加onConfigurationChanged回调方法。

//重写该方法,用于监听系统设置的更改,主要是监控屏幕方向的更改
	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		// TODO Auto-generated method stub
		super.onConfigurationChanged(newConfig);
		String screen=newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE?"横向屏幕":"竖向屏幕";
		Log.d("screen","onconfigurationchanged");
		ori.setText(screen);
		Toast.makeText(this, "系统方向发生改变"+"\n修改后的屏幕方向为:"+screen, Toast.LENGTH_LONG).show();
	}
}

2)在Manifest.xml文件中设置Activity可以监听屏幕方向发生改变的事件

  <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            <span style="color:#FF6666;">android:configChanges="orientation|screenSize"</span>
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
运行结果:




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值