TextView 不显示搜索栏
Posted
技术标签:
【中文标题】TextView 不显示搜索栏【英文标题】:TextView not Displaying Seekbar 【发布时间】:2018-01-07 06:58:37 【问题描述】:我现在刚刚遇到这个问题,我不知道如何解决这个问题。我创建了一个应用程序,可以计算四门学科(科学、数学、英语和编程)的平均值。用户可以使用搜索栏输入学生的成绩(搜索栏的最小值为 50%)。我还添加了切换按钮,可以包含或不包含选择的主题。问题是当我运行应用程序时,当我移动搜索栏时,文本视图(应该是显示搜索栏进度的那个)没有显示正确的值。它停留在 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_
android:layout_
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_
android:layout_
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_
android:layout_
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_
android:layout_
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_
android:layout_
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_
android:layout_
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_
android:layout_
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_
android:layout_
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_
android:layout_
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_
android:layout_
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_
android:layout_
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_
android:layout_
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_
android:layout_
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_
android:layout_
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_
android:layout_
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_
android:layout_
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 + "%");
);
希望各位能帮帮我,提前谢谢你们
【问题讨论】:
【参考方案1】:您已将android:max="50"
设置为SeekBar
。
所以它会寻求最大值到 50。
改成android:max="100"
现在 seekbar 可以在 0 到 100 之间搜索。所以你必须为onProgressChanged
制定逻辑。
【讨论】:
android:max="50"
这是你 xml 中的内容
我需要将搜索栏启动到 50% 而不是 0%
好的,让我们说清楚。如果您从 50 开始,那么您如何期望它进一步移动,因为 max 是 50 。 ?您不能在 SEekbar 中设置锚进度。所以它总是会从 0% 移动到 100% 。只需将其设置为 0 到 100 。然后将 step 设置为 2。
还有其他方法可以让我从 50% 而不是 0% 开始吗?以上是关于TextView 不显示搜索栏的主要内容,如果未能解决你的问题,请参考以下文章