簡體   English   中英

我想用文本視圖創建一個計數器

[英]i want to create a counter with a text view

我想創建一個帶有文本視圖的計數器,並希望每15次單擊即可更改文本視圖中的文本,這就是我為創建計數器而寫的內容........我有兩個文本視圖。 ...一個沒有 的點擊次數和另一個要顯示的文字,我想使用“ if”將計數器設置為0(計數器為tvx)並更改文本(tvx2) )以“再點擊15個”

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

你可以這樣,使用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();
    }
    @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
                                   }

                               }



                           }


    );


}

}

您好,如果有幫助,請嘗試此

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

我不太了解您想要什么,但是如果您希望每15次點擊增加一次計數器,只需在代碼中添加條件代碼,如下所示:

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


        }



    }


    );


}

希望能為您服務

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");
                }
            }
        });


    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM