質問

Hello stackoverflow users, I am stuck into a problem. I have created a custom number picker in android, eclipse.I put a limitation for number picker to don't go more than 35 and less than 10.The problem is, when I press continuous the button for + or - and I reach the limit, the toast continuously appear on the screen where I setted it up.The time I setted is Short so it's like 4-5 secconds of appearing and there is a little spark where toasts changes( for example: one toast disspear another already is there or now appearing I don't know )

I have the longclicklistener function here:

add.setOnLongClickListener(new View.OnLongClickListener() {

        @Override
        public boolean onLongClick(View v) {
            mAutoIncrement = true;
            repeatUpdateHandler.post(new RptUpdater() );
            return false;
        }
    });
    add.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            if( (event.getAction()==MotionEvent.ACTION_UP || event.getAction()==MotionEvent.ACTION_CANCEL) && mAutoIncrement )
                mAutoIncrement = false;
                return false;
        }
    });
    sub.setOnLongClickListener(new View.OnLongClickListener() {

        @Override
        public boolean onLongClick(View v) {
            mAutoDecrement = true;
            repeatUpdateHandler.post(new RptUpdater() );
            return false;
        }
    });
    sub.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            if( (event.getAction()==MotionEvent.ACTION_UP || event.getAction()==MotionEvent.ACTION_CANCEL) && mAutoDecrement )
                mAutoDecrement = false;
                return false;
        }
    });

and the decrement and increment function with the Toast is here:

public void increment(){
        if(counter<35){
        counter++;
        display.setText( "" + counter+"°C");
        showup.setText(" "+counter+"°C");
        }
        else
        {
            Context context = getApplicationContext();
            CharSequence text = "Maximum value is 35°C!";
            int duration = Toast.LENGTH_SHORT;
            final Toast toast = Toast.makeText(context, text, duration);
            toast.show();
            toast.setGravity(Gravity.TOP, 0, 100);
        }
    }
    public void decrement(){
        if(counter>10){
        counter--;
        display.setText( "" + counter+"°C");
        showup.setText(" "+counter+"°C");
        }
        else
        {
            Context context = getApplicationContext();
            CharSequence text = "Minimum value is 10°C!";
            int duration = Toast.LENGTH_SHORT;
            final Toast toast = Toast.makeText(context, text, duration);
            toast.show();
            toast.setGravity(Gravity.BOTTOM, 0, 50);
        }

    }

If anyone need to take a look on the whole code here is it:

package com.example.symbol_temp;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.ActionBarActivity;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {
    Integer counter=20;
    Button add,sub;



    static int REP_DELAY = 50;
    public TextView display,showup;
    private Handler repeatUpdateHandler = new Handler();
    private boolean mAutoIncrement = false;
    private boolean mAutoDecrement = false;
    class RptUpdater implements Runnable {
        public void run() {
            if( mAutoIncrement ){
                increment();
                repeatUpdateHandler.postDelayed( new RptUpdater(), REP_DELAY );
            } else if( mAutoDecrement ){
                decrement();
                repeatUpdateHandler.postDelayed( new RptUpdater(), REP_DELAY );
            }
        }
    }

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

        add = (Button) findViewById(R.id.plus);
        sub = (Button) findViewById(R.id.minus);
        display = (TextView) findViewById(R.id.showtemperature);
        showup = (TextView) findViewById(R.id.showmeup);

        showup.setText(" "+counter+"°C");
        display.setText(""+counter+"°C");
    add.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
        if(counter<35){
            counter++;
            display.setText( "" + counter+"°C");
            showup.setText(" "+counter+"°C");
            }
        else{
            Context context = getApplicationContext();
            CharSequence text = "Maximum value is 35°C!";
            int duration = Toast.LENGTH_SHORT;
            final Toast toast = Toast.makeText(context, text, duration);
            toast.show();
            toast.setGravity(Gravity.TOP, 0, 100);
            }
        }
    });

    sub.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {

        if(counter>10){
            counter--;
            display.setText( "" + counter+"°C");
            showup.setText(" "+counter+"°C");
            }
        else{
            Context context = getApplicationContext();
            CharSequence text = "Minimum value is 10°C!";
            int duration = Toast.LENGTH_SHORT;
            final Toast toast = Toast.makeText(context, text, duration);
            toast.show();
            toast.setGravity(Gravity.BOTTOM, 0, 50);
        }
        }

    });
    add.setOnLongClickListener(new View.OnLongClickListener() {

        @Override
        public boolean onLongClick(View v) {
            mAutoIncrement = true;
            repeatUpdateHandler.post(new RptUpdater() );
            return false;
        }
    });
    add.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            if( (event.getAction()==MotionEvent.ACTION_UP || event.getAction()==MotionEvent.ACTION_CANCEL) && mAutoIncrement )
                mAutoIncrement = false;
                return false;
        }
    });
    sub.setOnLongClickListener(new View.OnLongClickListener() {

        @Override
        public boolean onLongClick(View v) {
            mAutoDecrement = true;
            repeatUpdateHandler.post(new RptUpdater() );
            return false;
        }
    });
    sub.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            if( (event.getAction()==MotionEvent.ACTION_UP || event.getAction()==MotionEvent.ACTION_CANCEL) && mAutoDecrement )
                mAutoDecrement = false;
                return false;
        }
    });
    if(savedInstanceState != null)
            counter = savedInstanceState.getInt("myCounter");
            display.setText( "" + counter+"°C");
            showup.setText(" "+counter+"°C");
    }
    public void increment(){
        if(counter<35){


counter++;
    display.setText( "" + counter+"°C");
    showup.setText(" "+counter+"°C");
    }
    else
    {
        Context context = getApplicationContext();
        CharSequence text = "Maximum value is 35°C!";
        int duration = Toast.LENGTH_SHORT;
        final Toast toast = Toast.makeText(context, text, duration);
        toast.show();
        toast.setGravity(Gravity.TOP, 0, 100);
    }
}
public void decrement(){
    if(counter>10){
    counter--;
    display.setText( "" + counter+"°C");
    showup.setText(" "+counter+"°C");
    }
    else
    {
        Context context = getApplicationContext();
        CharSequence text = "Minimum value is 10°C!";
        int duration = Toast.LENGTH_SHORT;
        final Toast toast = Toast.makeText(context, text, duration);
        toast.show();
        toast.setGravity(Gravity.BOTTOM, 0, 50);
    }

}
protected void onSaveInstanceState(Bundle savedInstance) {
       super.onSaveInstanceState(savedInstance); 
       savedInstance.putInt("myCounter",counter); 
       }

}

I have readed the stackoverflow topic here: How to Customize Toast In Android but didn't helped me ( at the suggestion of creating project name )

役に立ちましたか?

解決

You could only repeat if the number is not already the max for add / min for subtract:

class RptUpdater implements Runnable {
    public void run() {
        if( mAutoIncrement &&  counter<35){
            increment();
            repeatUpdateHandler.postDelayed( new RptUpdater(), REP_DELAY );
        } else if( mAutoDecrement && counter>10){
            decrement();
            repeatUpdateHandler.postDelayed( new RptUpdater(), REP_DELAY );
        }
    }
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top