开发者

Updating a textview based on user input

I have multiple SeekBars which the user can manipulate to change various values in a text field, I have those values so they will update in real time.

I can't figure out how to get those numbers (I need to do basic math like add, multiply etc) into a new textview that updates simultaneously.

I'll try to post some code, but I'm super new to java, android, and even this site so don't be surprised when none of it makes sense. :-P

jumping down to (what I think) is the important code.

    SeekBar sbc = (SeekBar) findViewById(R.id.seekBar4);
    final TextView tvc = (TextView) findViewById(R.id.textView10);
    sbc.setMax(200);
    sbc.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }

        @Override
    开发者_运维问答    public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress,
                boolean fromUser) {
            tvc.setText(String.valueOf(progress-100));
        }

    });

    //set up chance bar and text

    TextView tvp = (TextView) findViewById(R.id.textView14);
    tvp.setText(tvc.getText().toString());

As you can see my seek bar goes from -100 to 100... pretty proud of that one (which took about forever for me to google/research lol)

Also the tvp.setText(tvc.getText().toString()); only pulls out the initial value of tvc

Thanks in advance and in the meantime I'll keep playing with it.


I'm not sure why you're setting tvp and tvc to show the same value, but if you want tvp to update dynamically to match the value of tvc, then you need to move your last bit of code inside your Listener, i.e.:

@Override
public void onProgressChanged(SeekBar seekBar, int progress,
        boolean fromUser) {
    tvc.setText(String.valueOf(progress-100));
    TextView tvp = (TextView) findViewById(R.id.textView14);
    tvp.setText(tvc.getText().toString());
}

Or more cleanly:

@Override
public void onProgressChanged(SeekBar seekBar, int progress,
        boolean fromUser) {
    String progress = String.valueOf(progress-100)
    tvc.setText(progress);
    tvp.setText(progress); // move the findViewById outside this method, and put it with tvc
}

Alternatively, if you're having difficulty changing the text in tvc, this may be because you've declared it as final. If this is the case, try making tvc a non-final member variable of your class, i.e. put it outside any method, at the beginning of your class like this:

public class MyClass extends Activity {

    private TextView tvc;
    ...

EDIT:

Thanks for the extra info! Try something like this:

public class MyClass extends Activity {

    private TextView tvc;
    private TextView tvd;
    private TextView tvp;

    ... // somewhere here you need to get the TextViews from the Layout
    ... // missing out lots of code here and skipping straight to onProgressChanged for first seekBar

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress,
            boolean fromUser) {
        String progress1 = String.valueOf(progress-100)
        tvc.setText(progress1);
        String progress2 = tvd.getText().toString;
        String result = resultOfSomeCalculationUsingTheInputs();
        tvp.setText(result);
    }
}

In your other SeekBar you would do something similar (set the text of tvd, get the text of tvc, perform calculation, set result as text of tvp)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜