所以我正在编写的应用程序有两个(到目前为止)在片段中的搜索栏。评分栏位于主布局文件中。我需要做的是根据 SeekBars 的值更改评分栏进度。这是代码,所以你有一个更好的主意:
片段 Java 文件
package com.example.tipquiz;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RatingBar;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
public class QuizFragment extends Fragment implements OnSeekBarChangeListener {
SeekBar fqBar, cBar;
TextView fqTV, cTV;
TextView fqTVP, cTVP;
RatingBar rb;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.quiz_fragment_layout, container,
false);
//Inflate the Main layout to access the RatingBar
View mainView = inflater.inflate(R.layout.activity_main, container, false);
//FoodQuality Bar
fqBar = (SeekBar) view.findViewById(R.id.seekBarFQ);
fqBar.setOnSeekBarChangeListener(this);
fqTV = (TextView) view.findViewById(R.id.textViewFQ2);
fqTVP = (TextView) view.findViewById(R.id.textViewFQP);
//Cleanliness Bar
cBar = (SeekBar) view.findViewById(R.id.seekBarC);
cBar.setOnSeekBarChangeListener(this);
cTV = (TextView) view.findViewById(R.id.textViewC2);
cTVP = (TextView) view.findViewById(R.id.textViewCP);
rb = (RatingBar) mainView.findViewById(R.id.ratingBar1);
return view;
}
@Override
public void onProgressChanged(SeekBar arg0, int progress, boolean fromUser) {
switch(arg0.getId()) {
case R.id.seekBarFQ:
if(progress > 70 && progress < 90){
fqTV.setText("Above Average - ");
rb.setRating(4);
}else if(progress > 40 && progress <= 70){
fqTV.setText("Average - ");
}else if(progress <= 40){
fqTV.setText("Awful - ");
}else if(progress > 90){
fqTV.setText("Perfect - ");
}
fqTVP.setText(progress + "%");
break;
case R.id.seekBarC:
if(progress > 70 && progress < 90){
cTV.setText("Above Average - ");
}else if(progress > 40 && progress <= 70){
cTV.setText("Average - ");
}else if(progress <= 40){
cTV.setText("Awful - ");
}else if(progress > 90){
cTV.setText("Perfect - ");
}
cTVP.setText(progress + "%");
break;
}
}
@Override
public void onStartTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
}
@Override
public void onStopTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
}
}
主班
package com.example.tipquiz;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.TextView;
public class MainActivity extends Activity implements OnRatingBarChangeListener{
// Testing Stuff to show the rating value, will need to use later for maths
static RatingBar rb;
TextView tv;
// The Image used as the DropDown button, Rotate code below
ImageView dropDownButton;
Boolean hasRotated = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dropDownButton = (ImageView) findViewById(R.id.dropDownButton);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
FragmentManager fm = getFragmentManager();
QuizFragment qf = new QuizFragment();
public void dropDown(View view){
if(hasRotated == false){
FragmentTransaction ft = fm.beginTransaction();
ft.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out);
dropDownButton.setRotation(90);
ft.add(R.id.quizFragment, qf);
ft.show(qf);
ft.commit();
hasRotated = true;
}else if(hasRotated == true){
FragmentTransaction ft = fm.beginTransaction();
ft.setCustomAnimations(android.R.animator.fade_out, android.R.animator.fade_out);
dropDownButton.setRotation(0);
hasRotated = false;
ft.remove(qf);
ft.commit();
}
}
@Override
public void onRatingChanged(RatingBar ratingBar, float rating,
boolean fromTouch) {
// final int numStars = ratingBar.getNumStars();
tv.setText(rating + "/5.0");
}
// http://www.compiletimeerror.com/2013/08/android-rating-bar-example.html#.U7SZ5fldXm4
}
我尝试了很多方法,而我所做的大多数方法都会在 Eclipse 中出现错误或崩溃。我现在使用的方法不会崩溃或给出任何错误,但评级栏不会在它应该改变的时候改变。
我也尝试过简单地使用 MainActivity.rb.setProgress(4); 在 if 语句中,但我遇到了这个崩溃:
D/(938): HostConnection::get() 新主机连接建立 0xb7ca38d0, tid 938 W/EGL_emulation(938): eglSurfaceAttrib 未实现 D/OpenGLRenderer(938): 启用调试模式 0 E/InputEventReceiver(938): 异常调度输入事件。关闭 VM W/dalvikvm(938): threadid=1: thread exiting with uncaught exception (group=0xb3b0db90) D/dalvikvm(938): GC_FOR_ALLOC freed 94K, 6% free 3184K/3360K, paused 31ms, total 37ms E/AndroidRuntime (938):致命异常:主 E/AndroidRuntime(938):进程:com.example.tipquiz,PID:938 E/AndroidRuntime(938):java.lang.NullPointerException E/AndroidRuntime(938):在 com.example。 tipquiz.QuizFragment.onProgressChanged(QuizFragment.java:50) E/AndroidRuntime(938): 在 android.widget.SeekBar.onProgressRefresh(SeekBar.java:91) E/AndroidRuntime(938): 在 android.