In my android application I have a TextView
and a SeekBar
. Both the values are used for calculating result on a button click.
The pr开发者_如何学JAVAoblem is how to capture the SeekBar
value and convert it into String for the calculation.
The code is below:
class clicker implements Button.OnClickListener {
public void onClick(View v) {
String a,b;
Integer vis;
a = txtbox3.getText().toString();
b = slider1.getContext().toString();
vis = (Integer.parseInt(a)*Integer.parseInt(b))/100;
tv.setText(vis.toString());
}
}
Please Help. Thanks in advance.
You are looking for the method getProgress() of the ProgressBar
class as SeekBar
is a subclass of ProgressBar
.
So basically it would be something like that.
int value = seekBar.getProgress();
Also I don't really understand why you want to convert an int to a String just so you can convert that String to an Integer later. It does not make any sense.
You can store the progress value into a global integer variable. Then you can use it easily where ever you want. Try this Code. It will help you...
int p=0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final TextView textView=(TextView) findViewById(R.id.t1);
final SeekBar seek=(SeekBar) findViewById(R.id.seekBar1);
final Button button=(Button) findViewById(R.id.button1);
seek.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {
// TODO Auto-generated method stub
p=progress;
}
});
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String a = textView.getText().toString();
vis = (Integer.parseInt(a)*p)/100;
text.setText(vis.toString());
}
});
// variable in top
String infos;
//-----`enter code here`
// defaultSeekBar (assign by your id)
defaultSeekBar.setOnProgressChangedListener(new ProgressListener(){
@Override
public void invoke(int i) {
Log.d("VALUE",""+ i);
infos = String.valueOf(defaultSeekBar.getProgress());
textViewDefaultSeek.setText(infos);
}
});
精彩评论