0

我正在尝试应用 onTouchListener 但我遇到了一些代码问题,没有 switchcase 它正在工作,当应用 switch case 时它不是,下面是我的代码,下面是 switch case 代码

 if (phoneNo != null && !phoneNo.equals("")
                    && !phoneNo.equalsIgnoreCase("null")) {
                textPhone.setText(phoneNo);
                textPhone.setVisibility(View.VISIBLE);
                phImage.setVisibility(View.VISIBLE);

                phImage.setImageResource(R.drawable.phone);
                phImage.setTag(phoneNo);

                phImage.setOnTouchListener(new OnTouchListener() {



                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        switch (event.getAction()) {
                        case MotionEvent.ACTION_DOWN: {
                        String phone = (String) ((ImageView) v).getTag();
                        Log.d(TAG, "onTouch phone--" + phone);
                        utils.dailPhone(v.getContext(), phone);
                        return false;
                    }
                        }}

                 else {
                phImage.setVisibility(View.GONE);
                textPhone.setVisibility(View.GONE);

            }
                break;
                    case MotionEvent.ACTION_MOVE:
                        break;
                    case MotionEvent.ACTION_UP:
                        break;
                    }

                    return false;
                }

下面没有开关

phImage.setOnTouchListener(new OnTouchListener() {



                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    String phone = (String) ((ImageView) v).getTag();
                    Log.d(TAG, "onTouch phone--" + phone);
                    utils.dailPhone(v.getContext(), phone);
                    return false;
                }
            });

        } else {
            phImage.setVisibility(View.GONE);
            textPhone.setVisibility(View.GONE);

        }
4

1 回答 1

2

您的 switch-case 语法完全错误。尝试类似:

public boolean onTouch(MotionEvent event) {
    int eventaction = event.getAction();

    switch (eventaction) {
        case MotionEvent.ACTION_DOWN: 
            // finger touches the screen
            String phone = (String) ((ImageView) v).getTag();
            Log.d(TAG, "onTouch phone--" + phone);
            utils.dailPhone(v.getContext(), phone);
            return false;
            break;

        case MotionEvent.ACTION_MOVE:
            // finger moves on the screen
            break;

        case MotionEvent.ACTION_UP:   
            // finger leaves the screen
            break;
    }

    // tell the system that we handled the event and no further processing is required
    return true; 
}
于 2013-01-21T10:11:27.940 回答