Android应用,自定义数字选择器,烤面包连续出现长按

时间:2014-04-16 06:50:03

标签: java android numberpicker

Hello stackoverflow用户,我遇到了问题。 我在android中创建了一个自定义数字选择器,eclipse。我对数字选择器的限制不要超过35且小于10.问题是,当我按下连续按钮+或 - 然后我到达限制,吐司不断出现在我设置它的屏幕上。我设置的时间是短的,所以它就像4-5秒出现并且有一点点火花在哪里改变(例如:一个toast disspear另一个已经存在或现在出现我不知道)

我在这里有longclicklistener函数:

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

和Toast的递减和递增函数在这里:

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

    }

如果有人需要查看整个代码,请访问:

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

}

我在这里重读了stackoverflow主题:How to Customize Toast In Android但是没有帮助我(根据创建项目名称的建议)

1 个答案:

答案 0 :(得分:2)

如果数字不是减去加/减的最大值,则只能重复:

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