安卓学习-界面-事件-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+"\n";
                if(con.keyboard==Configuration.KEYBOARD_NOKEYS)
                    str=str+"键盘类型:普通键盘类型"+"\n";
                else if (con.keyboard==Configuration.KEYBOARD_12KEY)
                    str=str+"键盘类型:小键盘"+"\n";    
                else
                    str=str+"键盘类型:无"+"\n";
            
                str=str+"地区:"+con.locale+"\n";
                str=str+"移动国家码:"+con.mcc+"\n";
                str=str+"移动网络码:"+con.mnc+"\n";
                
                if(con.navigation==Configuration.NAVIGATION_NONAV)
                    str=str+"方向导航设备:无导航"+"\n";
                else if(con.navigation==Configuration.NAVIGATION_DPAD)
                    str=str+"方向导航设备:导航"+"\n";
                else if(con.navigation==Configuration.NAVIGATION_TRACKBALL)
                    str=str+"方向导航设备:轨迹导航"+"\n";
                else if(con.navigation==Configuration.NAVIGATION_WHEEL)
                    str=str+"方向导航设备:滚轮导航"+"\n";
                else
                    str=str+"方向导航设备:无"+"\n";
                
                if(con.orientation==Configuration.ORIENTATION_LANDSCAPE)
                    str=str+"屏幕方向:横向"+"\n";
                else if(con.orientation==Configuration.ORIENTATION_PORTRAIT)
                    str=str+"屏幕方向:竖向"+"\n";
                else
                    str=str+"屏幕方向:无"+"\n";
                
                if(con.touchscreen==Configuration.TOUCHSCREEN_NOTOUCH)
                    str=str+"触摸方式:无触摸"+"\n";
                else if(con.touchscreen==Configuration.TOUCHSCREEN_FINGER)
                    str=str+"触摸方式:手指触摸"+"\n";
                else
                    str=str+"触摸方式:无"+"\n";
    
                tv.setText(str);
                
            }
        });
    }
    
    //监听配置修改
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        
        String str="";
        if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE)
            str=str+"屏幕方向:横向"+"\n";
        else if(newConfig.orientation==Configuration.ORIENTATION_PORTRAIT)
            str=str+"屏幕方向:竖向"+"\n";
        else
            str=str+"屏幕方向:无"+"\n";
        
        Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT).show();
        
    }
}
View Code

 

posted on 2014-11-01 13:49  weijj  阅读(423)  评论(0编辑  收藏  举报

导航