安卓SeekBar编码

3

我正在创建一个用于阅读文档的Android应用程序。在这个应用中,我使用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);
        }
    }

}

我在这个活动中有多个文本视图。如果我像这样调用每一个 textViewOne.setTextSize((float) fontSize); textViewTwo.setTextSize((float) fontsize); 我已经在 onCreate 中通过 id 调用了 textView。因此,我需要使用 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();
    }

}

你实际想要什么?你想将SeekBar的进度显示到进度TextView并更改字体大小吗? - Ferdous Ahamed
是的,Ferdous,我想要将SeekBar的进度显示到进度TextView上,并改变整个应用程序的字体大小。 - Rey
然后,您应该将fontsize值存储到SharedPreference中,并在需要设置fontsize的地方检索。请参见下面的答案。我已经详细解释了它。 - Ferdous Ahamed
1个回答

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);
    }
}

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 中,从 SharedPreference 获取 FontSize 的值,并使用 textView.setTextSize() 方法更改 TextView 的字体大小。

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);
    }
}

希望这会有所帮助~

谢谢回复。请问最后一段代码应该放在哪里?我有点困惑。 - Rey
你在说哪一个?>> # 从其他活动中,通过使用SharedPreference获取FontSize值,并使用textView.setTextSize()方法更改TextView的FontSize。 - Ferdous Ahamed
假设您有另一个带有TextView的Activity,并且您想要设置在首选项中存储的字体大小。那么您可以在Activity的onCreate()方法中应用此代码。 - Ferdous Ahamed
我在另一个活动中添加了上述行。但是当我改变seekbar的大小时仍然没有任何反应。 - Rey
是的,我已经应用了...请看上面的代码。我已经在其中一个活动中应用了那段代码。请检查并回复。 - Rey
显示剩余9条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接