android - 搜索栏进度更新

标签 android seekbar

背景:我有一个 Activity ,带有一个 Textview 和一个按钮。单击该按钮会运行 SetValue(),它会打开一个包含 Seekbar 的对话框,该对话框又会设置 TextView 的值。代码如下:

    public void SetValue(View view){
    ShowDialog();
    }


    public void ShowDialog() {

    final AlertDialog.Builder popDialog = new AlertDialog.Builder(this);
    final SeekBar seek = new SeekBar(this);
    int value = 0;
    try {
        value = Integer.parseInt(tv.getText().toString());
    } catch(NumberFormatException nfe) {
       System.out.println("Could not parse " + nfe);
    } 

    seek.setProgress(value);
    seek.setMax(100);
    popDialog.setView(seek);
    seek.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser){
    //Update textview value
    tv.setText("Value : " + progress);
    }
        public void onStartTrackingTouch(SeekBar arg0) {
        // TODO Auto-generated method stub
        }
        public void onStopTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub
        }
    });
    // Button 
    popDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
            }
            });
    popDialog.create();
    popDialog.show();
    }

所以它工作正常,显示初始 TextView 进度,并让我使用搜索栏更新它。

问题: 最初设置值后,如果我尝试再次使用该方法来更改文本,则搜索栏上的进度每次都会从 0 开始,而此时它应该处于先前选择的值/当前 TextView 值。

此外,我可以设置条形的最大值,我可以设置最小值吗?即-100?如何设置搜索栏的范围?

谢谢大家的建议!

更新 1: 我在类开始时初始化了变量,因此消除了 int value = 0; 行,但问题仍然存在 - 搜索栏始终打开时会显示 TextView 原始文本值的进度。

最佳答案

首先,当用户完成“寻找” SeekBar 时,您需要保留您的值。 。我们在 onStopTrackingTouch 中执行此操作:

public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser){
    //Update textview value
    tv.setText("Value : " + progress);
}
public void onStartTrackingTouch(SeekBar arg0) {
    // TODO Auto-generated method stub
}
public void onStopTrackingTouch(SeekBar seekBar) {
    // Update your preferences only when the user has finished moving the seek bar
    SharedPreferences prefs = getContext().getSharedPreferences("mySharedPrefsFilename", Context.MODE_PRIVATE);
    // Don't forget to call commit() when changing preferences.
    prefs.edit().putInt("seekBarValue", seekBar.getProgress()).commit();
}

现在,您希望在创建对话框时显示此持久值 - 因此您需要检索它。你可以这样做:

int value = 0;
SharedPreferences prefs = getContext().getSharedPreferences("mySharedPrefsFilename", Context.MODE_PRIVATE);
value = prefs.getInt("seekBarValue", 0); // 0 is default

seek.setProgress(value);

// You also need to update your TextView to show the saved value
tv.setText("Value : " + value);

您可以将访问包装到 SharedPreferences在类里面;这是确保您使用相同的首选项文件名(本例中为 "mySharedPrefsFilename")和相同的首选项键来存储/检索值(本例中为 "seekbarValue")的一种方法。

关于android - 搜索栏进度更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21222535/

相关文章:

java - 如何从 Activity 类之外更改 android 墙纸?

android - 如何去下一张卡片点击 Android 中的 CardView 项目

android - 具有多种颜色的搜索栏或进度条

android - 更流畅的 Android SeekBar

java - 错误无法将字符串转换为整数

android - "ask, don' t look”如何在Android中应用?

android - 允许或拒绝将我的应用程序复制到 SD 卡?

android - 如何使 Seekbar 不可触摸?

android - android(xml)中的普通搜索栏和离散搜索栏有什么区别