我现在遇到了这个问题,但我无法弄清楚如何解决这个问题。我创建了一个应用程序,可以计算四个科目(科学,数学,英语和编程)的平均值。用户可以使用搜索栏输入学生的成绩(搜索栏的最小值为50%)。我还添加了切换按钮,可以选择是否包含主题。问题是当我运行应用程序时,textview(应该是显示搜索栏进度的那个)在我移动搜索栏时没有显示正确的值。它保持在50.我做错了吗?这是我的代码
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.elijah.comp3labexercise9.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Student Grade Evaluation"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="25dp"
android:gravity="center_horizontal"
android:id="@+id/textView" />
<SeekBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/seekSci"
android:layout_marginTop="58dp"
android:layout_below="@id/textView"
android:layout_toLeftOf="@+id/toggleSci"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:max="50"
android:progress="0"
android:indeterminate="false" />
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New ToogleButton"
android:id="@+id/toggleSci"
android:layout_alignBottom="@id/seekSci"
android:layout_alignRight="@id/textView"
android:layout_alignEnd="@id/textView"
android:textOff="Not Included"
android:textOn="Included"
android:checked="false" />
<SeekBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/seekMath"
android:layout_marginTop="52dp"
android:layout_below="@id/seekSci"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="@id/seekSci"
android:layout_alignEnd="@id/seekSci"
android:max="50"
android:progress="0"
android:indeterminate="false" />
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New ToogleButton"
android:id="@+id/toggleMath"
android:layout_alignBottom="@id/seekMath"
android:layout_alignLeft="@id/toggleSci"
android:layout_alignStart="@id/toggleSci"
android:textOff="Not Included"
android:textOn="Included"
android:checked="false" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Science: 50%"
android:id="@+id/txtSci"
android:layout_alignTop="@id/toggleSci"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="@id/seekSci"
android:layout_alignEnd="@id/seekSci" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Mathematics: 50%"
android:id="@+id/txtMath"
android:layout_alignTop="@id/toggleMath"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="@id/seekMath"
android:layout_alignEnd="@id/seekMath" />
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New ToogleButton"
android:id="@+id/toggleEng"
android:layout_below="@id/toggleMath"
android:layout_alignLeft="@id/toggleMath"
android:layout_alignStart="@id/toggleMath"
android:layout_marginTop="27dp"
android:textOff="Not Included"
android:textOn="Included"
android:checked="false" />
<SeekBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/seekEng"
android:layout_alignBottom="@id/toggleEng"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="@id/seekMath"
android:layout_alignEnd="@id/seekMath"
android:max="50"
android:progress="0"
android:indeterminate="false" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="English: 50%"
android:id="@+id/txtEng"
android:layout_alignTop="@id/toggleEng"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="@id/seekEng"
android:layout_alignEnd="@id/seekEng" />
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New ToogleButton"
android:id="@+id/toggleProg"
android:layout_below="@id/toggleEng"
android:layout_alignLeft="@id/toggleEng"
android:layout_alignStart="@id/toggleEng"
android:layout_marginTop="32dp"
android:textOff="Not Included"
android:textOn="Included"
android:checked="false" />
<SeekBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/seekProg"
android:layout_alignBottom="@id/toggleProg"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="@id/seekEng"
android:layout_alignEnd="@id/seekEng"
android:max="50"
android:progress="0"
android:indeterminate="false" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Programming: 50%"
android:id="@+id/txtProg"
android:layout_above="@id/seekProg"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="@id/seekProg"
android:layout_alignEnd="@id/seekProg" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CALCULATE"
android:id="@+id/btnCalc"
android:layout_below="@id/seekProg"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="31dp"
android:layout_alignRight="@+id/seekProg"
android:layout_alignEnd="@id/toggleProg" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Average: No Grade"
android:id="@+id/txtResult"
android:layout_below="@id/btnCalc"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="@id/btnCalc"
android:layout_alignEnd="@id/btnCalc"
android:layout_marginTop="10dp"
android:textColor="#2a34f4"/>
MainActivity.java
package com.example.elijah.comp3labexercise9;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.ToggleButton;
public class MainActivity extends AppCompatActivity {
ToggleButton togSci, togMath, togEng, togPro;
SeekBar skSci, skMath, skEng, skPro;
TextView etSci, etMath, etEng, etPro, etRes;
Button calc;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
togSci = (ToggleButton) findViewById (R.id.toggleSci);
togMath = (ToggleButton) findViewById (R.id.toggleMath);
togEng = (ToggleButton) findViewById (R.id.toggleEng);
togPro = (ToggleButton) findViewById (R.id.toggleProg);
skSci = (SeekBar) findViewById (R.id.seekSci);
skMath = (SeekBar) findViewById (R.id.seekMath);
skEng = (SeekBar) findViewById (R.id.seekEng);
skPro = (SeekBar) findViewById (R.id.seekProg);
etSci = (TextView) findViewById (R.id.txtSci);
etMath = (TextView) findViewById (R.id.txtMath);
etEng = (TextView) findViewById (R.id.txtEng);
etPro = (TextView) findViewById (R.id.txtProg);
etRes = (TextView) findViewById (R.id.txtResult);
calc = (Button) findViewById(R.id.btnCalc);
calc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int countGrd = 0;
float totalGrd = 0;
if (togSci.isChecked()) {
countGrd++;
totalGrd = totalGrd + skSci.getProgress() + 50;
}
if (togEng.isChecked()) {
countGrd++;
totalGrd = totalGrd + skEng.getProgress() + 50;
}
if (togMath.isChecked()) {
countGrd++;
totalGrd = totalGrd + skMath.getProgress() + 50;
}
if (togPro.isChecked()) {
countGrd++;
totalGrd = totalGrd + skPro.getProgress() + 50;
}
if (countGrd == 0) {
etRes.setText("Average: No Grade");
}else{
float ave = totalGrd / countGrd;
etRes.setText("Average: "+ String.format("%.2f%%" , ave));
}
}
});
}
private void seekbarLisHander() {
skSci.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
int progressChanged = 0;
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean b) {
progressChanged = progress;
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
int grd = progressChanged + 50;
etSci.setText("Science: " + grd + "%");
}
});
skMath.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
int progressChanged = 0;
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean b) {
progressChanged = progress + 50;
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
int grd = progressChanged + 50;
etMath.setText("Mathematics: " + grd + "%");
}
});
skEng.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
int progressChanged = 0;
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean b) {
progressChanged = progress;
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
int grd = progressChanged + 50;
etEng.setText("English: " + grd + "%");
}
});
skPro.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
int progressChanged = 0;
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean b) {
progressChanged = progress;
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
int grd = progressChanged + 50;
etPro.setText("Programming: " + grd + "%");
}
});
}
}
我希望你能帮助我并提前感谢你
答案 0 :(得分:0)
您已将android:max="50"
设为SeekBar
。
所以它会寻求最多50个。
将其更改为android:max="100"
现在,搜索栏可以在0到100之间寻找。所以你必须为onProgressChanged
制作逻辑。