Android事件处理之三 响应的系统设置的事件

本文详细介绍了如何通过系统Configuration接口获取和调整应用与系统设置的交互,包括屏幕方向、键盘类型、触摸屏方式等关键信息。通过实例代码展示了如何在活动中动态获取并显示这些设置,并提供了在系统设置更改时自动响应的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

应用程序随系统设置进行调整,如屏幕方向、判断系统方向的导航设备。有时也需让应用监听系统的动态配置

3.4.1 Configuration 简介
调用如下方法获取系统的Configuration对象:
Configuration cfg = getResource().getConfiguration();
获得系统Configuration对象后,有如下常用属性配置:
public float fontScale
获取当前用户设置的字体缩放因子
public int keyboard
获取当前设置所关联的键盘类型
KEYBOARD_NOKEYS
KEYBOARD_QWERTY(普通电脑键盘)
KEYBOARD_12KEY(只有12个键的小键盘)
public int keyboardHidden
返回一个boolean值标识当前键盘是否可用(软键盘也在检测范围之内)
public Locale locale
获取当前的Locale
public int mcc
获取移动信号的国家码
public int mnc
网络码
public int navigation
判断系统上方向导航的类型
NAVIGATION_NONAV
NAVIGATION_DPAD
NAVIGATION_TRACKBALL
NAVIGATION_WHEEL
public int orientation
获取屏幕方向
ORIENTATION_LANDSCAPE
ORIENTATION_PORTRAIT
ORIENTATION_SQUARE
public int touchscreen
获取系统触摸屏方式
TOUCHSCREEN_NOTOUCH
TOUCHSCREEN_STYLUS
TOUCHSCREEN_FINGER
获取手机信息示例代码:
xml

   
<EditText
       
android:id="@+id/ori"
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
       
android:editable="false"
       
android:cursorVisible="false"
        android:hint="display screen orientation" />
java代码:
public class MainActivity extends ActionBarActivity {

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 source)
        {
        Configuration cfg = getResources().getConfiguration();
        String screen = cfg.
orientation ==
        Configuration.
ORIENTATION_LANDSCAPE?
       
"LANDSCAPE SCREEN":"PORTRAINT SCREEN";
        String mncCode = cfg.
mnc + "";
        String naviName = cfg.
orientation ==
        Configuration.
NAVIGATION_NONAV?
       
"no navigation control":
        cfg.
orientation ==
        Configuration.
NAVIGATION_WHEEL?
       
"滚轮控制":
                cfg.
orientation ==
                Configuration.
NAVIGATION_DPAD?
               
"no orientation control":
               
"DPAD control orientation";
       
navigation.setText(naviName);
        String touchName = cfg.
touchscreen ==
        Configuration.
TOUCHSCREEN_NOTOUCH?
       
"no touch screen":"support bar";
       
ori.setText(screen);
       
mnc.setText(mncCode);
       
        }
        });
    }
}

3.4.2 重写onConfigurationChanged响应系统设置更改
监听系统设置的更改——重写Activity的onConfigurationChanged(Configuration newConfig)方法,该方法是一个基于回调的事件处理方法:当系统设置更改时,该方法会自动触发。
为了在程序中动态地更改系统设置,可以调用Activity的setRequestedOrientation(int)方法来修改屏幕方向
示例代码:
public class MainActivity extends ActionBarActivity {

   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_main);
        Button bn = (Button) findViewById(R.id.
bn);
        bn.setOnClickListener(
new OnClickListener()
        {
       
@Override
       
public void onClick(View source)
        {
        Configuration config =getResources().getConfiguration();
       
if(config.orientation == Configuration.ORIENTATION_LANDSCAPE)
        {
        MainActivity.
this.setRequestedOrientation(
        ActivityInfo.
SCREEN_ORIENTATION_PORTRAIT);
        }
       
if(config.orientation == Configuration.ORIENTATION_PORTRAIT)
        {
        MainActivity.
this.setRequestedOrientation(
        ActivityInfo.
SCREEN_ORIENTATION_LANDSCAPE);
        }
        }
        });
    }
若想监听系统设置关于屏幕更改的设置,需重写onConfigurationChanged(Configuration newConfig)方法,另外,还需更改配置文件内设置:

       
<activity
            android:configChanges="orientation"
main.java内重写代码:
 @Override
    public void onConfigurationChanged(Configuration newConfig)
    {
   
super.onConfigurationChanged(newConfig);
    String screen = newConfig.
orientation ==
    Configuration.
ORIENTATION_LANDSCAPE?
   
"landscape screen":"portrait screen";
    Toast.makeText(
this, "screen orientation change to " + screen, Toast.LENGTH_LONG).show();
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值