Configuration类

本文介绍了一个Android应用程序中如何使用Configuration类来获取设备配置信息,如屏幕方向、导航方式、移动网络码等,并通过实例展示了如何在按钮点击事件中更新UI显示这些配置信息。

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

package com.crazyit.ui.configurationdemo;

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

/**
 * Configuration类 专门用于描述手机设备上的配置信息,
 * 这些配置信息 既包括用户特定的配置项,也包括系统的动态设备配置.
 */
public class MainActivity extends Activity {
    TextView ori, nav, touch, mnc;
    Button btn;

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

        ori = (TextView) findViewById(R.id.ori);
        nav = (TextView) findViewById(R.id.nav);
        touch = (TextView) findViewById(R.id.touch);
        mnc = (TextView) findViewById(R.id.mnc);
        btn = (Button) findViewById(R.id.button);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取系统的 configuraction
                Configuration cfg = getResources().getConfiguration();

                String screen = cfg.orientation == Configuration.ORIENTATION_LANDSCAPE
                        ? "横向屏幕" : "竖向屏幕";
                String mncCode = cfg.mnc + "";

                String navName = cfg.orientation == Configuration.NAVIGATION_NONAV
                        ? "没有方向控制" :
                        cfg.orientation == Configuration.NAVIGATION_WHEEL
                                ? "滚轮控制方向" :
                                cfg.orientation == Configuration.NAVIGATION_DPAD
                                        ? "方向键控制" : "轨迹球控制方向";
                nav.setText(navName);
                mnc.setText(mncCode);
                touch.setText(screen);
            }
        });

    }
}

布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.crazyit.ui.configurationdemo.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="方向"
        android:id="@+id/ori" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="导航"
        android:id="@+id/nav" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="触摸屏"
        android:id="@+id/touch" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="移动信号网络码"
        android:id="@+id/mnc" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="获取手机信息"
        android:id="@+id/button" />
</LinearLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值