2014-10-13 1 views
0

내 애플리케이션의 RatingBar가 변경되면 현재 RatingBar 값에 따라 텍스트보기 (팁 값)를 특정 숫자로 변경할 수 있기를 원합니다. onRatingChanged 메서드를 사용하여이 시도했다 그러나 작동하지 않는 것. 또한 사용자가 등급 막대를 터치하지 않더라도 등급 막대를 변경하는 내 앱에 퀴즈 섹션이 있기 때문에 팁 값이 RatingBar 값을 기반으로 변경되기를 바랍니다. 이것이 명확하지 않으면 미안합니다. 설명하기가 어렵습니다.onRatingChanged가 제대로 작동하지 않습니다.

내가 원하는 것은 : 사용자가 등급 표시 줄을 변경

  1. 는 이에 따라 전을 끝 값을 변경합니다. 별 5 개 -> 20 팁 값
  2. 따라 다음

내 코드 ratingbar 변경 (사용자가 퀴즈 활동을 사용하기 때문에) 팁 값을 변경할 수 있습니다 : 당신은하지 않았다

package com.example.tipquiz; 

import com.google.ads.AdRequest; 
import com.google.ads.AdView; 

import android.app.Activity; 
import android.app.FragmentManager; 
import android.app.FragmentTransaction; 
import android.content.Intent; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.ImageView; 
import android.widget.RatingBar; 
import android.widget.RatingBar.OnRatingBarChangeListener; 
import android.widget.RelativeLayout; 
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 tipsTV; 

    private AdView adview; 

    ImageView greyPlus, greyMinus, greyPlus2, greyMinus2; 

    TextView peopleDiningTV, peopleDiningTitle; 
    float peopleDining = 2; 

    TextView tipValue; 
    float tipValueFloat = 10; 

    TextView subtotal, total; 
    TextView subtotalTitle, totalTitle; 

    TextView epp, eppTitle; 
    Button done; 
    ImageView settingsButton; 

    // Elements for hiding and such 
    static RelativeLayout rl; 

    public static int rating = 3; 

    // 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); 
     rb = (RatingBar) findViewById(R.id.ratingBar1); 
     rb.setRating(rating); 

     tipsTV = (TextView) findViewById(R.id.textView2); 

     tipValue = (TextView) findViewById(R.id.tipText); 

     greyPlus = (ImageView) findViewById(R.id.greyPlus); 
     greyMinus = (ImageView) findViewById(R.id.greyMinus); 

     greyPlus2 = (ImageView) findViewById(R.id.greyPlus2); 
     greyMinus2 = (ImageView) findViewById(R.id.greyMinus2); 

     peopleDiningTV = (TextView) findViewById(R.id.textViewPeople); 
     peopleDiningTitle = (TextView) findViewById(R.id.TextView02); 

     subtotal = (TextView) findViewById(R.id.subtotalText); 
     subtotalTitle = (TextView) findViewById(R.id.subtotalTitle); 

     total = (TextView) findViewById(R.id.totalText); 
     totalTitle = (TextView) findViewById(R.id.totalTitle); 

     epp = (TextView) findViewById(R.id.eppText); 
     eppTitle = (TextView) findViewById(R.id.eppTitle); 

     done = (Button) findViewById(R.id.buttonDone); 
     settingsButton = (ImageView)findViewById(R.id.settingsButton); 

     greyPlus.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       tipValueFloat++; 
       tipValue.setText(tipValueFloat + "%"); 
      } 
     }); 
     greyMinus.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       if (tipValueFloat >= 1) { 
        tipValueFloat--; 
        tipValue.setText(tipValueFloat + "%"); 
       } 
       if (tipValueFloat == 0) { 
        tipValue.setText("No Tip."); 
       } 
      } 
     }); 

     greyPlus2.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       peopleDining++; 
       peopleDiningTV.setText(peopleDining + ""); 
      } 
     }); 
     greyMinus2.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       if (peopleDining > 1) { 
        peopleDining--; 
        peopleDiningTV.setText(peopleDining + ""); 
       } 
      } 
     }); 

     // Look up the AdView as a resource and load a request. 
     AdView adView = (AdView) this.findViewById(R.id.adView); 
     AdRequest adRequest = new AdRequest(); 
     adView.loadAd(adRequest); 
    } 

    @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; 

      // Hiding Elements, so they don't show through the fragment 
      tipsTV.setVisibility(View.INVISIBLE); 
      greyPlus.setVisibility(View.INVISIBLE); 
      greyMinus.setVisibility(View.INVISIBLE); 
      tipValue.setVisibility(View.INVISIBLE); 

      greyPlus2.setVisibility(View.INVISIBLE); 
      greyMinus2.setVisibility(View.INVISIBLE); 
      peopleDiningTV.setVisibility(View.INVISIBLE); 
      peopleDiningTitle.setVisibility(View.INVISIBLE); 

      subtotal.setVisibility(View.INVISIBLE); 
      subtotalTitle.setVisibility(View.INVISIBLE); 
      total.setVisibility(View.INVISIBLE); 
      totalTitle.setVisibility(View.INVISIBLE); 

      epp.setVisibility(View.INVISIBLE); 
      eppTitle.setVisibility(View.INVISIBLE); 

      done.setVisibility(View.INVISIBLE); 
      settingsButton.setVisibility(View.INVISIBLE); 
     } 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(); 

      // Hiding Elements, so they don't show through the fragment 
      tipsTV.setVisibility(View.VISIBLE); 
      greyPlus.setVisibility(View.VISIBLE); 
      greyMinus.setVisibility(View.VISIBLE); 
      tipValue.setVisibility(View.VISIBLE); 

      greyPlus2.setVisibility(View.VISIBLE); 
      greyMinus2.setVisibility(View.VISIBLE); 
      peopleDiningTV.setVisibility(View.VISIBLE); 
      peopleDiningTitle.setVisibility(View.VISIBLE); 

      subtotal.setVisibility(View.VISIBLE); 
      subtotalTitle.setVisibility(View.VISIBLE); 
      total.setVisibility(View.VISIBLE); 
      totalTitle.setVisibility(View.VISIBLE); 

      epp.setVisibility(View.VISIBLE); 
      eppTitle.setVisibility(View.VISIBLE); 

      done.setVisibility(View.VISIBLE); 
      settingsButton.setVisibility(View.VISIBLE); 
     } 
    } 

    public void openSettings(View view) { 
     Intent intent = new Intent(this, SettingsActivity.class); 
     startActivity(intent); 
    } 

    @Override 
    public void onRatingChanged(RatingBar ratingBar, float rating, 
      boolean fromTouch) { 
     if(ratingBar.getRating() == 5){ 
      tipValue.setText(20+""); 
     } 
    } 

    public void done(View view) { 
     float subtotalCost = Float.parseFloat(subtotal.getText().toString()); 
     float tip = tipValueFloat/100; 
     float totalCost = (subtotalCost * tip) + subtotalCost; 

     epp.setText(totalCost/peopleDining + ""); 

     total.setText(totalCost + ""); 

    } 
} 

답변

2

등급 표시 줄에 대한 ratingBarChangeListener를 설정하십시오. 당신은 이런 그렇게 할 수 (에서 onCreate() 메소드이 추가) :

rb.setOnRatingBarChangeListener(new OnRatingBarChangeListener() { 

     @Override 
     public void onRatingChanged(RatingBar ratingBar, float rating, 
       boolean fromUser) { 
      // Do what you want 
      if(rating == 5){ 
       tipValue.setText(20+""); 
      } 
     } 
    }); 
+0

이가에서 onCreate에 가야합니까? –

+0

예, onCreate에 있습니다. (rb = (RatingBar) findViewById (R.id.ratingBar1);) – erkams

+0

감사 표시자를 초기화 한 직후에이 코드 부분을 추가 할 수 있습니다. 이 작품! 내가 2 개 이상의 조건이있는 경우 –

관련 문제