2017-04-18 84 views
2

我正在創建一個閱讀文檔的android應用程序。在這個應用程序中,我使用seekbar來調整字體的大小。我不能在代碼中實現這一點。任何人都可以協助我的代碼。安卓Seekbar編碼

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    android:visibility="visible" 
    tools:context="islamicdawahkuwait.allahyaar.SettingsActivity"> 

     <LinearLayout 
      android:orientation="vertical" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content"> 

     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="App Font Size :-" 
      android:textSize="20dp" 
      android:scrollbars="vertical" 
      /> 

    <SeekBar 
     android:id="@+id/seekBar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="36dp"/> 

    <TextView 
     android:id="@+id/progress" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="36dp" 
     android:gravity="center"/> 

    </LinearLayout> 

</ScrollView> 

我已經在下面提到的其他活動中添加了代碼。但我仍然沒有得到任何東西。請協助

package islamicdawahkuwait.allahyaar; 

import android.content.Intent; 
import android.content.SharedPreferences; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.MenuItem; 
import android.widget.TextView; 

public class C2Activity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_c2); 

     android.support.v7.app.ActionBar actionBar = getSupportActionBar(); 
     actionBar.setDisplayHomeAsUpEnabled(true); 
     actionBar.setHomeAsUpIndicator(R.drawable.ic_arrow1); 

     SharedPreferences sharedPreferences = getSharedPreferences("SAMPLE_PREFERENCE", MODE_PRIVATE); 
     int fontSize = sharedPreferences.getInt("FONT_SIZE", 14); 

     TextView textView = (TextView) findViewById(R.id.textView); 
     textView.setTextSize((float) fontSize); 


    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item){ 
     switch (item.getItemId()){ 
      case android.R.id.home: 
       Intent intent = new Intent(this,ContentActivity.class); 
       intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); 
       startActivity(intent); 
       return true; 
      default: 
       return super.onOptionsItemSelected(item); 
     } 
    } 
} 

我已經申請在設置活動點3下

package islamicdawahkuwait.allahyaar; 

import android.content.Intent; 
import android.content.SharedPreferences; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.MenuItem; 
import android.widget.EditText; 
import android.widget.SeekBar; 
import android.widget.Toast; 
import android.widget.SeekBar.OnSeekBarChangeListener; 
import android.widget.TextView; 

import org.w3c.dom.Text; 

import static islamicdawahkuwait.allahyaar.R.id.seekBar; 

public class SettingsActivity extends AppCompatActivity { 
    private SeekBar seekBar; 
    private TextView textProgress; 
    private TextView textParagraph; 

    private static final int progress = 14; 

    SharedPreferences sharedPreferences; 
    SharedPreferences.Editor editor; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_settings); 

     android.support.v7.app.ActionBar actionBar = getSupportActionBar(); 
     actionBar.setDisplayHomeAsUpEnabled(true); 
     actionBar.setHomeAsUpIndicator(R.drawable.ic_arrow1); 

     seekBar = (SeekBar) findViewById(R.id.seekBar); 
     textProgress = (TextView) findViewById(R.id.progress); 
     textParagraph = (TextView) findViewById(R.id.textParagraph); 

     sharedPreferences = getSharedPreferences("SAMPLE_PREFERENCE", MODE_PRIVATE); 
     editor = sharedPreferences.edit(); 

     updateView(progress); 

     seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { 
      @Override 
      public void onProgressChanged(SeekBar seekBar, int i, boolean b) { 

       updateView(progress + i); 

       editor.putInt("FONT_SIZE", progress + i); 
       editor.commit(); 

      } 

      @Override 
      public void onStartTrackingTouch(SeekBar seekBar) { 

      } 

      @Override 
      public void onStopTrackingTouch(SeekBar seekBar) { 

      } 
     }); 
    } 

    public void updateView(int fontSize) { 
     textProgress.setText(String.valueOf(fontSize)); 
     textParagraph.setTextSize((float) fontSize); 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item){ 
     switch (item.getItemId()){ 
      case android.R.id.home: 
       Intent intent = new Intent(this,ContentActivity.class); 
       intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); 
       startActivity(intent); 
       return true; 
      default: 
       return super.onOptionsItemSelected(item); 
     } 
    } 

} 

我在這個活動有多個textviews提到。如果我調用每個像textViewOne.setTextSize((float)fontSize); textViewTwo.setTextSize((float)fontsize); 我已經調用了textView byid onCreate,如下所示。因此,我需要使用btn.setTextSize((float)fontSize); ??這是對的嗎。請教育我。由於

package islamicdawahkuwait.allahyaar; 

import android.content.DialogInterface; 
import android.content.Intent; 
import android.os.Bundle; 
import android.os.Handler; 
import android.support.v7.app.AlertDialog; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.View; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.Button; 
import android.view.View.OnClickListener; 
import android.widget.TextView; 
import android.widget.Toast; 

import static android.R.attr.id; 

public class ContentActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_content); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     TextView btn = (TextView) findViewById(R.id.tv1); 
     btn.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       Intent i = new Intent(getApplicationContext(),Activity1.class); 
       i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); 
       startActivity(i); 
      } 
     }); 

     TextView btn2 = (TextView) findViewById(R.id.tv2); 
     btn2.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       Intent i = new Intent(getApplicationContext(),SolActivity.class); 
       i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); 
       startActivity(i); 
      } 
     }); 

     TextView btn3 = (TextView) findViewById(R.id.tv3); 
     btn3.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       Intent i = new Intent(getApplicationContext(),C2Activity.class); 
       i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); 
       startActivity(i); 
      } 
     }); 

     TextView btn4 = (TextView) findViewById(R.id.tv4); 
     btn4.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       Intent i = new Intent(getApplicationContext(),C3Activity.class); 
       i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); 
       startActivity(i); 
      } 
     }); 

     TextView btn5 = (TextView) findViewById(R.id.tv5); 
     btn5.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       Intent i = new Intent(getApplicationContext(),C4Activity.class); 
       i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); 
       startActivity(i); 
      } 
     }); 

     TextView btn6 = (TextView) findViewById(R.id.tv6); 
     btn6.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       Intent i = new Intent(getApplicationContext(),C5Activity.class); 
       i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); 
       startActivity(i); 
      } 
     }); 

     TextView btn7 = (TextView) findViewById(R.id.tv7); 
     btn7.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       Intent i = new Intent(getApplicationContext(),C6Activity.class); 
       i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); 
       startActivity(i); 
      } 
     }); 

     TextView btn8 = (TextView) findViewById(R.id.tv8); 
     btn8.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       Intent i = new Intent(getApplicationContext(),C7Activity.class); 
       i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); 
       startActivity(i); 
      } 
     }); 

     TextView btn9 = (TextView) findViewById(R.id.tv9); 
     btn9.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       Intent i = new Intent(getApplicationContext(),C8Activity.class); 
       i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); 
       startActivity(i); 
      } 
     }); 

     TextView btn10 = (TextView) findViewById(R.id.tv10); 
     btn10.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       Intent i = new Intent(getApplicationContext(),C9Activity.class); 
       i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); 
       startActivity(i); 
      } 
     }); 

     TextView btn11 = (TextView) findViewById(R.id.tv11); 
     btn11.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       Intent i = new Intent(getApplicationContext(),C10Activity.class); 
       i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); 
       startActivity(i); 
      } 
     }); 

     TextView btn12 = (TextView) findViewById(R.id.tv12); 
     btn12.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       Intent i = new Intent(getApplicationContext(),C11Activity.class); 
       i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); 
       startActivity(i); 
      } 
     }); 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_content, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 

     switch (item.getItemId()) { 

      case R.id.action_settings: 
       if (item.isChecked()) 
        item.setChecked(false); 
       else 
        item.setChecked(true); 
       Intent i = new Intent(getApplicationContext(),SettingsActivity.class); 
       i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); 
       startActivity(i); 
       return true; 

      case R.id.action_about: 
       if (item.isChecked()) 
        item.setChecked(false); 
       else 
        item.setChecked(true); 
       displayToast(" Allah Yaar\n\nVersion 1.0.0"); 
       return true; 

      case R.id.action_exit: 
       if(item.isChecked()) 
        item.setChecked(false); 
       else 
        item.setChecked(true); 
       new AlertDialog.Builder(this).setIcon(android.R.drawable.ic_dialog_alert).setTitle("Exit") 
         .setMessage("Are you sure?") 
         .setPositiveButton("yes", new DialogInterface.OnClickListener() { 
          @Override 
          public void onClick(DialogInterface dialog, int which) { 

           Intent intent = new Intent(Intent.ACTION_MAIN); 
           intent.addCategory(Intent.CATEGORY_HOME); 
           intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
           startActivity(intent); 
           finish(); 
          } 
         }).setNegativeButton("no", null).show(); 
       return true; 
      default: 
       return super.onOptionsItemSelected(item); 
     } 
    } 

    void displayToast(String message){ 
     Toast.makeText(this, message, Toast.LENGTH_SHORT).show(); 
    } 

    @Override 
    public void onBackPressed() { 
     new AlertDialog.Builder(this).setIcon(android.R.drawable.ic_dialog_alert).setTitle("Exit") 
       .setMessage("Are you sure?") 
       .setPositiveButton("yes", new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 

         Intent intent = new Intent(Intent.ACTION_MAIN); 
         intent.addCategory(Intent.CATEGORY_HOME); 
         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
         startActivity(intent); 
         finish(); 
        } 
       }).setNegativeButton("no", null).show(); 
    } 

} 
+0

你真正想要什麼?你想顯示seekbar進度來改進textview並改變字體大小嗎? – FAT

+0

是Ferdous,我想顯示seekbar進度來改進textview並更改整個應用程序的字體大小 – Rey

+0

然後,您應該將fontsize值存儲到SharedPreference並回顧需要設置字體大小的位置。請參閱下面的答案。我已經解釋了它的詳細信息 – FAT

回答

0

1.更新您的activity_settings XML如下:

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:visibility="visible"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="16dp" 
     android:orientation="vertical"> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="8dp" 
      android:text="App Font Size :-" 
      android:textSize="16sp" 
      android:scrollbars="vertical" /> 

     <SeekBar 
      android:id="@+id/seekBar" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="16dp" /> 

     <TextView 
      android:id="@+id/progress" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="16dp" 
      android:text="0" 
      android:textSize="36sp" 
      android:gravity="center_horizontal" /> 

     <TextView 
      android:id="@+id/textParagraph" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="36dp" 
      android:text="This is a sample text."/> 

    </LinearLayout> 
</ScrollView> 

2.在你SettingsActivity,添加SeekBarChangeListenerSeekBarprogress值變化更新TextView

public class SettingsActivity extends AppCompatActivity { 

    private SeekBar seekBar; 
    private TextView textProgress; 
    private TextView textParagraph; 

    private static final int progress = 14; // Default font-size 14sp 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_settings); 

     seekBar = (SeekBar) findViewById(R.id.seekBar); 
     textProgress = (TextView) findViewById(R.id.progress); 
     textParagraph = (TextView) findViewById(R.id.textParagraph); 

     updateView(progress); 

     seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { 
      @Override 
      public void onProgressChanged(SeekBar seekBar, int i, boolean b) { 

       updateView(progress + i); 
      } 

      @Override 
      public void onStartTrackingTouch(SeekBar seekBar) { 

      } 

      @Override 
      public void onStopTrackingTouch(SeekBar seekBar) { 

      } 
     }); 
    } 

    public void updateView(int fontSize) { 
     textProgress.setText(String.valueOf(fontSize)); 
     textParagraph.setTextSize((float) fontSize); 
    } 
} 

OUTPUT:

enter image description here

3.如果你想FontSize值存儲到SharedPreference從其他ActivityFragment以後使用,那就試試這個存儲value

public class SettingsActivity extends AppCompatActivity { 

    private SeekBar seekBar; 
    private TextView textProgress; 
    private TextView textParagraph; 

    private static final int progress = 14; // Default font-size 14sp 

    SharedPreferences sharedPreferences; 
    SharedPreferences.Editor editor; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_settings); 

     seekBar = (SeekBar) findViewById(R.id.seekBar); 
     textProgress = (TextView) findViewById(R.id.progress); 
     textParagraph = (TextView) findViewById(R.id.textParagraph); 

     sharedPreferences = getSharedPreferences("SAMPLE_PREFERENCE", MODE_PRIVATE); 
     editor = sharedPreferences.edit(); 

     updateView(progress); 

     seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { 
      @Override 
      public void onProgressChanged(SeekBar seekBar, int i, boolean b) { 

       updateView(progress + i); 

       // Store font-size in preference 
       editor.putInt("FONT_SIZE", progress + i); 
       editor.commit(); 
      } 

      @Override 
      public void onStartTrackingTouch(SeekBar seekBar) { 

      } 

      @Override 
      public void onStopTrackingTouch(SeekBar seekBar) { 

      } 
     }); 
    } 

    public void updateView(int fontSize) { 
     textProgress.setText(String.valueOf(fontSize)); 
     textParagraph.setTextSize((float) fontSize); 
    } 
} 

4.C2Activity,獲取FontSize值從SharedPreference和更改TextView FontSize通過使用textView.setTextSize()方法。

public class C2Activity extends AppCompatActivity { 

    TextView textView; 
    SharedPreferences sharedPreferences; 

    int fontSize; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_c2); 

     android.support.v7.app.ActionBar actionBar = getSupportActionBar(); 
     actionBar.setDisplayHomeAsUpEnabled(true); 
     actionBar.setHomeAsUpIndicator(R.drawable.ic_arrow1); 

     textView = (TextView) findViewById(R.id.textView); 

     sharedPreferences = getSharedPreferences("SAMPLE_PREFERENCE", MODE_PRIVATE); 

    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 

     // Get font-size from preference   
     fontSize = sharedPreferences.getInt("FONT_SIZE", 14); 

     // Change font-size 
     textView.setTextSize((float) fontSize); 
    } 
} 

希望這將有助於〜

+0

感謝您的回覆。我可以知道在哪裏放置最後的代碼。我有點困惑。 – Rey

+0

哪一個?你在說什麼>>#從其他活動中,從SharedPreference獲取FontSize值,並使用textView.setTextSize()方法更改TextView FontSize。 – FAT

+0

是的,只有一個.. – Rey