K
K
kirawa2014-03-24 12:18:19
Android
kirawa, 2014-03-24 12:18:19

Toast - how to work with it correctly?

I hang a similar code on several buttons, changing the names of the buttons themselves accordingly. Here, if I hold down the button, it displays the text with its description, but if I hold down another button, it again displays the previous description. In general, I click on btn1 displays btn1, I click on btn2 displays btn1, I click again on bnt2 displays btn2.

boolean isDown = false;
    Timer timer;
    TimerTask task;
    int count;
 
    public  void start(){
        timer.schedule(task, 2,20);
    }
    public boolean onTouch(final View view, MotionEvent motionEvent) {
        switch (view.getId()){
            case R.id.imgBntAdd:
                switch (motionEvent.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        timer = new Timer();
                        task = new TimerTask() {
                            public void run() {
                                if(isDown){
                                    count++;
                                }
                                 runOnUiThread(new Runnable() {
                                    public void run() {
                                        if(count >= 100){
                                            int[] pos = new int[2];
                                            btnAdd.getLocationInWindow(pos);
                                            String s = btnAdd.getContentDescription().toString();
                                            Toast t =  Toast.makeText(context, s, Toast.LENGTH_SHORT);
                                            t.setGravity(Gravity.TOP | Gravity.LEFT, btnAdd.getRight() + 5 ,pos[1] + 20 );
                                            t.show();
                                            count = 0;
                                            isDown = false;
                                        }
                                    }
                                });
                            }
                        };
                        this.isDown = true;
                        start();
                        btnAdd.setBackgroundColor(color1);
                        this.time1 = System.currentTimeMillis() / 1000;
                        break;
 
                    case MotionEvent.ACTION_UP:
                        btnAdd.setBackgroundColor(color2);
                        this.isDown = false;
                        this.count = 0;
                        this.time2 = System.currentTimeMillis() / 1000;
                        if (time1 + 1 - time2 > 0){
                        startActivityNote();
                        }
                    default:
                        break;
                }
                break;

Help to understand, please.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
K
kirawa, 2014-03-24
@kirawa

Yes, not a lot, I don’t understand why you wrote this

G
GavriKos, 2014-03-24
@GavriKos

@kirawa Explain why such difficulties in starting? Maybe I don’t understand something, but why the timer, counters - it’s all sort of implemented directly in the toast.

K
kirawa, 2014-03-24
@kirawa

here is a button. I click on it, it is highlighted, if I hold it for more than a second, then the Toast notification is turned on, otherwise the transition to another Activity. And when I let go, the backlight also disappears.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question