textView当前进度在progressbar下面

时间:2014-05-19 13:01:54

标签: android

我试图让textView显示我当前进度条的进度,但我无法让它工作。有人可以检查一下并帮助我吗?我尝试在谷歌搜索但没有运气..

那是我的textView和progressBar

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:background="@drawable/bg"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="33dp"
    android:text="@string/progresas"
    android:textAppearance="?android:attr/textAppearanceLarge"
     android:textStyle="bold"
    android:textColor="#FFFFFF" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/button1"
    android:layout_toRightOf="@+id/tvProgress"
    android:textColor="#FFFFFF"
    android:textStyle="bold"
    android:text="@string/mili" />

<ProgressBar
    android:id="@+id/progressBar1"
    style="@android:style/Widget.ProgressBar.Horizontal"
    android:layout_width="216dp"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_below="@+id/textView1"
    android:layout_marginTop="90dp" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/progressBar1"
    android:layout_below="@+id/progressBar1"
    android:layout_marginTop="21dp"
    android:textColor="#FFFFFF"
    android:textStyle="bold"
    android:text="TextView" />

<SeekBar
    android:id="@+id/seekBar1"
    android:layout_width="237dp"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView2"
    android:layout_below="@+id/textView2"
    android:textColor="#FFFFFF"
    android:textStyle="bold"
    android:layout_marginTop="15dp" />

<TextView
    android:id="@+id/tvProgress"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/seekBar1"
    android:layout_below="@+id/seekBar1"
    android:layout_marginTop="18dp"
    android:textColor="#FFFFFF"
    android:textStyle="bold"
    android:text="TextView" />

<Button
    android:id="@+id/button1"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/tvProgress"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="32dp"
    android:onClick="onSaveButtonClicked"
    android:text="Gerti!" />

<TextView
    android:id="@+id/textView4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/textView2"
    android:layout_alignBottom="@+id/textView2"
    android:layout_toRightOf="@+id/textView2"
    android:textColor="#FFFFFF"
    android:textStyle="bold"
    android:text="@string/mili" />

    <requestFocus />

这是我的活动

ProgressBar editProgressBar;
SeekBar seekBar;
TextView tvProgress;
TextView textView2;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_progress);

   editProgressBar = (ProgressBar) findViewById(R.id.progressBar1);
   seekBar = (SeekBar) findViewById(R.id.seekBar1);
   seekBar.setOnSeekBarChangeListener(this);
   tvProgress = (TextView) findViewById(R.id.tvProgress); 
   textView2 = (TextView) findViewById(R.id.textView2); 

    setMaxProgress();
    setCurrentProgress();
}

@Override
protected void onResume() {
    setMaxProgress();
    setCurrentProgress();
    super.onResume();
}

private void setCurrentProgress() {
    SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
    int p = sharedPref.getInt("prefs_progress", -1);
    long time = sharedPref.getLong("prefs_progress_time", -1);
    if (p != -1) {
        Calendar now = Calendar.getInstance();
        Calendar pTime = Calendar.getInstance();
        pTime.setTimeInMillis(time);
        if (now.get(Calendar.DAY_OF_WEEK) == pTime.get(Calendar.DAY_OF_WEEK)) {
            editProgressBar.setProgress(p);
            seekBar.setMax(editProgressBar.getMax() - p);
        }
    }
}

private void setMaxProgress() {
    SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
    Float l = sharedPref.getFloat("prefs_liters", -1);
    if (l > -1) {
        int ml = (int) (l * 1000);
        editProgressBar.setMax(ml);
        seekBar.setMax(ml);
        tvProgress.setText(0 + "");
    }

}

public void onSaveButtonClicked(View view) {
    int p = seekBar.getProgress() + editProgressBar.getProgress();
    editProgressBar.setProgress(p);
    seekBar.setProgress(0);
    seekBar.setMax(editProgressBar.getMax() - p);

    SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
    SharedPreferences.Editor editor = sharedPref.edit();
    editor.putInt("prefs_progress", p);
    editor.putLong("prefs_progress_time", new Date().getTime());
    editor.commit();
}


@Override
public void onProgressChanged(SeekBar seekBar, int progress,
        boolean fromUser) {
    tvProgress.setText(progress + "");
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
    // TODO Auto-generated method stub

}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
    // TODO Auto-generated method stub

}

}

1 个答案:

答案 0 :(得分:0)

尝试添加此..先使 ml 全局变量

if (now.get(Calendar.DAY_OF_WEEK) == pTime.get(Calendar.DAY_OF_WEEK)) {
    editProgressBar.setProgress(p);
    seekBar.setMax(editProgressBar.getMax() - p);

    new Thread(new Runnable() {
    @Override
        public void run() {
            // TODO Auto-generated method stub
            try{
                while(p<ml){
                    Thread.sleep(1000); // You can removed this sleep line, if
                                //your update(change of value of p) takes time.
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            // TODO Auto-generated method stub
                            tvProgress.setText(String.valueOf(p));
                        }
                    });
                }
            }
            catch(Exception e){
        // handle exception     
            }
        }
    }).start();
}