Android Seekbar编码
Android Seekbar coding
我正在创建一个 android 应用程序来阅读文档。在此应用程序中,我使用搜索栏来调整字体的大小。我无法用代码实现这一点。任何人都可以帮助我的代码。
<?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>
我在另一个 activity 中添加了代码,如下所述。但我还是一无所获。请协助
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);
}
}
}
我在设置 activity 中应用了第 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);
}
}
}
我在这个 activity 中有多个文本视图。如果我把每一个都叫做 textViewOne.setTextSize((float) fontSize); textViewTwo.setTextSize((浮动)字体大小);
我已经调用了 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();
}
}
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
中,将 SeekBarChangeListener
添加到 SeekBar
并在 progress
时更新 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);
}
}
输出:
3. 如果您想将 FontSize
值存储到 SharedPreference
中供以后从其他 Activity
或 Fragment
中使用, 然后尝试存储 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
,从 SharedPreference
获取 FontSize
值并使用 [=29 更改 TextView FontSize
=]方法。
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);
}
}
希望对你有所帮助~
我正在创建一个 android 应用程序来阅读文档。在此应用程序中,我使用搜索栏来调整字体的大小。我无法用代码实现这一点。任何人都可以帮助我的代码。
<?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>
我在另一个 activity 中添加了代码,如下所述。但我还是一无所获。请协助
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);
}
}
}
我在设置 activity 中应用了第 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);
}
}
}
我在这个 activity 中有多个文本视图。如果我把每一个都叫做 textViewOne.setTextSize((float) fontSize); textViewTwo.setTextSize((浮动)字体大小); 我已经调用了 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();
}
}
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
中,将 SeekBarChangeListener
添加到 SeekBar
并在 progress
时更新 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);
}
}
输出:
3. 如果您想将 FontSize
值存储到 SharedPreference
中供以后从其他 Activity
或 Fragment
中使用, 然后尝试存储 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
,从 SharedPreference
获取 FontSize
值并使用 [=29 更改 TextView FontSize
=]方法。
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);
}
}
希望对你有所帮助~