您的位置:首页 > 其它

Configuration类响应系统设置的事件处置

2016-08-15 22:50 375 查看
 代码:

  activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text=" 欢迎光临手机之家 "
android:textColor="#00f"
android:textSize="15pt" />

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="显示屏幕方向"
android:id="@+id/ori"
android:layout_above="@+id/navigation"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true" />

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="显示手机方向控制设备"
android:id="@+id/navigation"
android:layout_above="@+id/touch"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true" />

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="显示触摸屏状态"
android:id="@+id/touch"
android:layout_above="@+id/mnc"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true" />

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="显示移动网络代号"
android:id="@+id/mnc"
android:layout_above="@+id/bn"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点击获取手机信息"
android:id="@+id/bn"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>


MainActivity.java
<span style="font-size:18px;">package com.example.haige.configuration;

import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity
{
EditText ori;
EditText navigation;
EditText touch;
EditText mnc;
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 View.OnClickListener() {
//为按钮绑定事件监听器
public void onClick(View source)
{
//获取系统的Configuration对象
Configuration cfg=getResources().getConfiguration();
String screen=cfg.orientation ==Configuration.ORIENTATION_LANDSCAPE?"横向屏幕":"竖向屏幕";
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);
}
});
}
}
</span>

运行截图:







 总结:

能自动获取Android上一些基本配置的类,感觉挺不错的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: