系统设置更改时间onConfigurationChanged

在前一个例子中我们看到了屏幕方向的更改,事实上,当屏幕方向改变是,就会发生onConfigurationChanged()事件;虽然可以在更改方向是显示要更改的方向,但是并无法取得更改后的宽高或更改后的结果,此时,就必须通过onConfigurationChanged()的心事事件进行处理。

onConfigurationChanged()方法是当系统发生系统设置改变之后所触发的事件,其中唯一的传入参数为Configuration对象,出来可以捕捉屏幕设置更改事件之外,也可扑捉其他系统设置更改事件,如隐藏键盘或打开键盘等。

public class EX05_23 extends Activity
{
  private TextView mTextView01;
  private Button mButton01;
  
  /* 屏幕宽高 */
  private int intScreenH,intScreenW;
  
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    
    /* 声明Display对象,以取得屏幕宽高 */
    final Display defaultDisplay = getWindow().getWindowManager().getDefaultDisplay();
    intScreenH= defaultDisplay.getHeight();
    intScreenW = defaultDisplay.getWidth();
    
    mButton01 = (Button)findViewById(R.id.myButton1); 
    mTextView01 = (TextView)findViewById(R.id.myTextView1);
    mTextView01.setText(Integer.toString(intScreenW)+"x"+Integer.toString(intScreenH));
    
    /* 当宽>高,表示为横式显示 */
    if(intScreenW > intScreenH)
    {
      /* Landscape */
      mButton01.setText(R.string.str_button2);
    }
    else
    {
      /* Portrait */
      mButton01.setText(R.string.str_button1);
    }
    
    /* 按钮事件处理切换宽高 */
    mButton01.setOnClickListener(new Button.OnClickListener()
    {
      @Override
      public void onClick(View v)
      {
        // TODO Auto-generated method stub
        intScreenH= defaultDisplay.getHeight();
        intScreenW = defaultDisplay.getWidth();
        
        /* 如果为Landscape */
        if(intScreenW > intScreenH)
        {
          /* Landscape => Portrait */
          setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
        else
        {
          /* Portrait => Landscape */
          setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        }
        
        /* 再一次取得屏幕宽高 */
        intScreenH= defaultDisplay.getHeight();
        intScreenW = defaultDisplay.getWidth();
        mTextView01.setText(Integer.toString(intScreenW)+"x"+Integer.toString(intScreenH));
      }
    });
  }  
  
  @Override
  public void onConfigurationChanged(Configuration newConfig)
  {
    // TODO Auto-generated method stub
    
    /* 覆写onConfigurationChanged事件,捕捉当设定之后的值 */
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
    {
      /* 趁着事件发生之后,变更按钮上的文字 */
      mButton01.setText(R.string.str_button2);
      mMakeTextToast
      (
        getResources().getText(R.string.str_onConf_LANDSCAPE).toString(),
        false
      );
    }
    
    /* 须设定configChanges属性才能捕捉onConfigurationChanged */
    if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
    {
      mButton01.setText(R.string.str_button1);
      mMakeTextToast
      (
        getResources().getText(R.string.str_onConf_PORTRAIT).toString(),
        false
      );
    }
    
    if (newConfig.keyboardHidden == Configuration.KEYBOARDHIDDEN_NO)
    {
     
    }
    super.onConfigurationChanged(newConfig);
  }
  
  public void mMakeTextToast(String str, boolean isLong)
  {
    if(isLong==true)
    {
      Toast.makeText(EX05_23.this, str, Toast.LENGTH_LONG).show();
    }
    else
    {
      Toast.makeText(EX05_23.this, str, Toast.LENGTH_SHORT).show();
    }
  }
}

 必须要在Activity里设置configChanges属性,以作为系统设置更改时要扑捉的事件,除此之外,还需要获得系统设置更改的权限(<uses-permission android:name="android.permission.CHANGE_CONFIGURATION"></uses-permission>)

<application
    android:icon="@drawable/icon"
    android:label="@string/app_name">
    <!-- 必須設定activity的configChanges屬性 -->
    <activity
      android:name=".EX05_23"
      android:label="@string/app_name"
      android:configChanges="orientation|keyboard">
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>
  </application>
  <uses-sdk android:minSdkVersion="8" />
  <!-- 必須設定CHANGE CONFIGURATION權限 -->
  <uses-permission android:name="android.permission.CHANGE_CONFIGURATION"></uses-permission>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值