【问题标题】:How to set multiple click action in a imageview and plus, minus button increment and decrement in android如何在imageview中设置多次点击动作以及android中的加号,减号按钮增量和减量
【发布时间】:2017-01-18 08:24:05
【问题描述】:

Hlw,

问题1: 我正在开发一个应用程序,其中有一个类似 ImageView 的收藏按钮,我单击并更改此图像资源(已完成),但我想在用户再次单击此图像视图并且图像保持不变时设置操作。

问题2: 我有两个图像按钮“加号”和“减号”我设置了条件,当用户单击 + 按钮中间 textview 增加 1 时,当它达到 10 时,按钮将不可点击,对于减号按钮,当 textviw 等于 = 0 然后它不起作用... 我是按条件完成的,但是当减号按钮达到 0 并且在单击加号按钮后,它会增加但不会减少它仍然无法点击... 我怎么解决这个问题? Image like this

productWrapper.plus.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

 //           Toast.makeText(context, "Plus", Toast.LENGTH_SHORT).show();
            try
            {
                String presentValStr= finalProductWrapper2.selectedQuantity.getText().toString();
                int presentIntVal=Integer.parseInt(presentValStr);
                presentIntVal++;
                if (presentIntVal>=10){
                    Toast.makeText(context,"You can select max 10 product",Toast.LENGTH_LONG).show();
                    finalProductWrapper2.plus.setEnabled(false);
                }
                finalProductWrapper2.selectedQuantity.setText(String.valueOf(presentIntVal));
            }
            catch(Exception e)
            {
                e.printStackTrace();
                Toast.makeText(context,"Error! please try again",Toast.LENGTH_LONG).show();
            }

        }
    });
        final ProductWrapper finalProductWrapper = productWrapper;
    productWrapper.heart.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            finalProductWrapper.heart.setImageResource(R.drawable.heart2);
        }
    });

【问题讨论】:

    标签: android android-layout onclicklistener imagebutton android-imagebutton


    【解决方案1】:

    第 1 点。

    你可以像下面这样使用布尔标志

    if(flag == true){
    flag = false;
    // Change your Image Resource here
    }
    else {
    flag = true;
    // Do your other action 
    }
    

    为减号按钮设置以下条件

    if(YOUR_POINTS >0){
    // Do your action
    }
    else {
    // Ignore
    }
    

    【讨论】:

    • 谢谢@Pavan,但它不能正常工作......请给我一个简单的演示代码......
    猜你喜欢
    • 2012-11-12
    • 1970-01-01
    • 2011-04-03
    • 2016-04-20
    • 1970-01-01
    • 2020-07-01
    • 1970-01-01
    • 2021-01-25
    • 1970-01-01
    相关资源
    最近更新 更多