非常容易学的android笔记6

Tue, Dec 20, 2022 2-minute read

Drawables, styles, and themes 功能

Drawables, styles, and themes教程

把基本的layout和元素都弄好,效果如图

TextView

before style

MainActivity.class

逻辑很简单

  • 数值 int mScore1, 2;
  • 数值 TextView mScoreText1, 2 ;
  • onCreate() 方法是,把mScoreText1, 2和 activity_main 的TextView 的id绑定
  • 把activity_main 的android:onClick=“decreaseScore”,“increaseScore” 方法完成
/**
* Method that handles the onClick of both the decrement buttons
* @param view The button view that was clicked
*/
public void decreaseScore(View view) {
  // Get the ID of the button that was clicked.
  int viewID = view.getId();
  switch (viewID) {
    // If it was on Team 1
    case R.id.decreaseTeam1:

统一修改样式 button_background.xml

 <style name="ScoreButtons" parent="AppTheme">
       <item 
         name="android:background">@drawable/button_background</item>
   </style>

   <style name="PlusButtons" parent="ScoreButtons">
       <item name="android:src">@drawable/ic_plus</item>
       <item name=
         "android:contentDescription">@string/plus_button_description</item>
   </style>

   <style name="MinusButtons" parent="ScoreButtons">
       <item name="android:src">@drawable/ic_minus</item>
       <item name=
        "android:contentDescription">@string/minus_button_description</item>
   </style>

改好后

TextView

style

白天黑夜模式

  • 名称写好
<string name="night_mode">Night Mode</string>
<string name="day_mode">Day Mode</string>
  • 菜单设置,有个黑夜模式
<item 
    android:id="@+id/night_mode"
    android:title="@string/night_mode"/>

MainActivity.class设置

  • 加onCreateOptionsMenu()方法

    • getMenuInflater().inflate(R.menu.main_menu, menu);
    • return super.onCreateOptionsMenu(menu)
  • 选择类型 onOptionsItemSelected(MenuItem item)方法

      + if (item.getItemId() == R.id.night_mode) {
    // Get the night mode state of the app.
    int nightMode = AppCompatDelegate.getDefaultNightMode();
    //Set the theme mode for the restarted activity.
    if (nightMode == AppCompatDelegate.MODE_NIGHT_YES) {
      AppCompatDelegate.setDefaultNightMode
                            (AppCompatDelegate.MODE_NIGHT_NO);
    } else {
      AppCompatDelegate.setDefaultNightMode
                           (AppCompatDelegate.MODE_NIGHT_YES);
    }
    // Recreate the activity for the theme change to take effect.
    recreate();
    

} ```

  • 这里可以变了的,但是呢,名称没变,一直是黑夜模式,所以回到onCreateOptionsMenu()方法中,把return super.onCreateOptionsMenu(menu) 改一下
int nightMode = AppCompatDelegate.getDefaultNightMode();
if (nightMode == AppCompatDelegate.MODE_NIGHT_YES) {
  menu.findItem(R.id.night_mode).setTitle(R.string.day_mode);
} else {
  menu.findItem(R.id.night_mode).setTitle(R.string.night_mode);
}
return true;
TextView

night view

onSaveInstanceState()

这个比较重要,就是你选择了屏幕后,没有这个方法,数值就从 0 开始

  • 记录数值 static final String STATE_SCORE_1 = “Team 1 Score”;
  • onSaveInstanceState(Bundle outState)
    • 储存数值
    @Override
protected void onSaveInstanceState(Bundle outState) {
  // Save the scores.
  outState.putInt(STATE_SCORE_1, mScore1);
  outState.putInt(STATE_SCORE_2, mScore2);
  super.onSaveInstanceState(outState);
}
  • onCreate()方法中,check savedInstanceState状态,如果不为空,从这里找数值
if (savedInstanceState != null) {
   mScore1 = savedInstanceState.getInt(STATE_SCORE_1);
   mScore2 = savedInstanceState.getInt(STATE_SCORE_2);

   // Set the score text views.
   mScoreText1.setText(String.valueOf(mScore1));
   mScoreText2.setText(String.valueOf(mScore2));
}