安卓学习-界面-事件-Configuration

安卓学习-界面-事件-Configuration

1.

获取系统基本信息,监听屏幕方向变更,需要实现onConfigurationChanged(Configuration newConfig),

若要监听方向变更,则必须在manifest里这样写,红色字的,否则无法监听

<activity
android:name=".MainActivity"
android:label="@string/app_name" android:configChanges="orientation|screenSize" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

MainActivity.java

public class MainActivity extends Activity {

    Button btn1;
    TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        btn1=(Button)findViewById(R.id.button1);
        tv=(TextView)findViewById(R.id.textView1);
        
        btn1.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                String str="";
                Configuration con=getResources().getConfiguration();
                str=str+"字体缩放因子:"+con.fontScale+"
";
                if(con.keyboard==Configuration.KEYBOARD_NOKEYS)
                    str=str+"键盘类型:普通键盘类型"+"
";
                else if (con.keyboard==Configuration.KEYBOARD_12KEY)
                    str=str+"键盘类型:小键盘"+"
";    
                else
                    str=str+"键盘类型:无"+"
";
            
                str=str+"地区:"+con.locale+"
";
                str=str+"移动国家码:"+con.mcc+"
";
                str=str+"移动网络码:"+con.mnc+"
";
                
                if(con.navigation==Configuration.NAVIGATION_NONAV)
                    str=str+"方向导航设备:无导航"+"
";
                else if(con.navigation==Configuration.NAVIGATION_DPAD)
                    str=str+"方向导航设备:导航"+"
";
                else if(con.navigation==Configuration.NAVIGATION_TRACKBALL)
                    str=str+"方向导航设备:轨迹导航"+"
";
                else if(con.navigation==Configuration.NAVIGATION_WHEEL)
                    str=str+"方向导航设备:滚轮导航"+"
";
                else
                    str=str+"方向导航设备:无"+"
";
                
                if(con.orientation==Configuration.ORIENTATION_LANDSCAPE)
                    str=str+"屏幕方向:横向"+"
";
                else if(con.orientation==Configuration.ORIENTATION_PORTRAIT)
                    str=str+"屏幕方向:竖向"+"
";
                else
                    str=str+"屏幕方向:无"+"
";
                
                if(con.touchscreen==Configuration.TOUCHSCREEN_NOTOUCH)
                    str=str+"触摸方式:无触摸"+"
";
                else if(con.touchscreen==Configuration.TOUCHSCREEN_FINGER)
                    str=str+"触摸方式:手指触摸"+"
";
                else
                    str=str+"触摸方式:无"+"
";
    
                tv.setText(str);
                
            }
        });
    }
    
    //监听配置修改
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        
        String str="";
        if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE)
            str=str+"屏幕方向:横向"+"
";
        else if(newConfig.orientation==Configuration.ORIENTATION_PORTRAIT)
            str=str+"屏幕方向:竖向"+"
";
        else
            str=str+"屏幕方向:无"+"
";
        
        Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT).show();
        
    }
}
View Code
原文地址:https://www.cnblogs.com/weijj/p/4067072.html