Android应用TextView无法显示SeekBar进度值问题求助
Hey there! Let's get those SeekBar text views updating properly when you drag them. I spotted two key issues in your code that are causing the problem:
1. The SeekBar Listener Method Isn't Being Called
You've written the seekbarLisHander() method that sets up all the SeekBar change listeners, but you never actually invoke this method in your onCreate() lifecycle method. That means none of the listeners are attached to the SeekBars—so dragging them does nothing to update the text views.
Fix: Add a call to seekbarLisHander() at the end of your onCreate() method, right after initializing all your views:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // ... all your existing view initialization code ... calc = (Button) findViewById(R.id.btnCalc); // Add this line to activate the SeekBar listeners seekbarLisHander(); calc.setOnClickListener(new View.OnClickListener() { // ... existing click listener code ... }); }
2. Incorrect Grade Calculation for Mathematics SeekBar
In the Mathematics SeekBar's listener, you're adding 50 twice: once in onProgressChanged() and again in onStopTrackingTouch(). This would result in grades way higher than intended (e.g., dragging the SeekBar to max would show 150% instead of 100%).
Fix: Update the Mathematics SeekBar listener to match the logic used for the other subjects:
skMath.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { int progressChanged = 0; @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean b) { progressChanged = progress; // Remove the +50 here } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { int grd = progressChanged + 50; // Only add 50 once here etMath.setText("Mathematics: " + grd + "%"); } });
Testing the Fix
After making these two changes:
- Drag any SeekBar, and the corresponding text view should now update to show the correct percentage (50% to 100%) when you stop dragging.
- The Mathematics grade calculation will also be accurate, matching the other subjects.
Let me know if you run into any other issues with the average calculation or toggle buttons!
内容的提问来源于stack exchange,提问作者pokoyo

