2017-07-18 2 views
0

Je veux créer un compteur avec une vue de texte et je veux pour chaque clic 15, pour changer le texte dans la vue de texte et c'est ce que j'ai écrit pour créer le compteur. ....... j'ai deux vues de texte .... un pour le non. de clics et l'autre pour le texte que je veux montrer ,,,,,,,,,,, donc je veux utiliser "si" pour mettre le compteur à 0 (le compteur est tvx) et changer le texte (tvx2) "cliquer 15 plus"Je veux créer un compteur avec une vue de texte

@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_tips_2); 

    btn = (Button) findViewById(R.id.bt); 
    txv = (TextView) findViewById(R.id.tx); 
    txv2 = (TextView) findViewById(R.id.tx2); 

    btn.setOnClickListener(new View.OnClickListener() 
    { 
     public int mCounter; 
     public Integer tx; 

     @Override 
     public void onClick(View v) { 
      mCounter++; 
      txv.setText(Integer.toString(mCounter)); 
     } 
    }); 
} 

Répondre

0
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_tips_2); 

    btn = (Button) findViewById(R.id.bt); 
    txv = (TextView) findViewById(R.id.tx); 
    txv2 = (TextView) findViewById(R.id.tx2); 
    public int mCounter=0; 
    public Integer tx =15; 


    btn.setOnClickListener(new View.OnClickListener() { 


           @Override 
           public void onClick(View v) { 
            mCounter++; 
            if(mCounter==tx){ 
             txv.setText(Integer.toString(mCounter)); 
             //reset the counter after 15 click 
             mCounter=0 
            } 

           } 



          } 


    ); 


} 

}

0

vous pouvez ainsi, utiliser le ValueAnimator

public void animateText(Integer startValue, Integer endValue) { 
     setStartValue(startValue); 
     setEndValue(endValue); 

     ValueAnimator animator = ValueAnimator.ofInt(startValue, endValue); 
     animator.setInterpolator(getInterpolator()); 
     animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 
      public void onAnimationUpdate(ValueAnimator animation) { 

       String cleanString = animation.getAnimatedValue().toString().replaceAll("[,]", ""); 
       BigDecimal parsed = new BigDecimal(cleanString); 
       String formatted = NumberFormat.getNumberInstance(Locale.US).format(parsed); 

       tv.setText(formatted); 
      } 
     }); 

     animator.setEvaluator(new TypeEvaluator<Integer>() { 
      public Integer evaluate(float fraction, Integer startValue, Integer endValue) { 
       return Math.round(startValue + (endValue - startValue) * fraction); 
      } 
     }); 

     animator.setDuration(getDuration()); 
     animator.start(); 
    } 
0

Bonjour Essayez ceci si elle peut aider

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_tips_2); 

    btn = (Button) findViewById(R.id.bt); 
    txv = (TextView) findViewById(R.id.tx); 
    txv2 = (TextView) findViewById(R.id.tx2); 


    btn.setOnClickListener(new View.OnClickListener() { 
     public int mCounter=0; 
     //initialize mCounter with 0 
     public Integer tx; 

     @Override 
     public void onClick(View v) { 
      if(mCounter==15){ 
      //check if mCounter equals 15 
       txv2.setText(Integer.toString("text to display after 15 click")); 
       //reset mCounter for another iteration 
       mCounter=0; 
      } 
      mCounter++; 
      //increment the mCounter 
      txv.setText(Integer.toString(mCounter)); 
     } 
    }); 
} 
0

Je suis pas vraiment comprendre ce que vous voulez, mais si vous voulez augmenter le compteur chaque 15 clic, ajoutez simplement si elle est conditionnelle dans votre code comme ceci:

btn.setOnClickListener(new View.OnClickListener() { 
      public int mCounter; 
      public Integer tx; 

      @Override 
      public void onClick(View v) { 
       mCounter++; 
       if(mCounter == 15){ 
        tx++; 
        txv.setText(String.valueOf(tx)); 
       } 
      }  
     } 
    ); 
0
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_tips_2); 

    btn = (Button) findViewById(R.id.bt); 
    txv = (TextView) findViewById(R.id.tx); 
    txv2 = (TextView) findViewById(R.id.tx2); 


    btn.setOnClickListener(new View.OnClickListener() { 
     public int mCounter; 
     public Integer tx; 
public int mCounter2; 

     @Override 
     public void onClick(View v) { 
      mCounter++; 
mCounter2++; 
      txv.setText(""+mCounter); 
if(mCounter2==15){ 
txv2.setText(""+mCounter2); 
     mCounter2=0; 
} 


     } 



    } 


    ); 


} 
0

J'espère et vous le servez

public class MainActivity extends AppCompatActivity { 
    private Button btn; 
    private TextView txv,txv2; 
    private int contador = 0; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     btn = (Button) findViewById(R.id.bt); 
     txv = (TextView) findViewById(R.id.tx); 
     txv2 = (TextView) findViewById(R.id.tx2); 

     btn.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
        ++contador; 
       if(contador <= 15){ 
       txv.setText(String.valueOf(contador)); 
       }else{ 
        txv2.setText("your text"); 
       } 
      } 
     }); 


    } 
}