系统设更改时间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、付费专栏及课程。

余额充值