【问题标题】:I want to keep increasing/decreasing Counters on Long Click on "+" or "-" Button.... Android setOnLongClickListener我想在长按“+”或“-”按钮时继续增加/减少计数器.... Android setOnLongClickListener
【发布时间】:2013-11-16 10:23:23
【问题描述】:

我能够在 setOnClickListener 的帮助下增加/减少相应的“-”或“-”按钮单击。 但是,如果我保持按下(长按)“+”或“-”按钮,我想保持增加/减少尊重计数器。就像在地图中一样,当我们触摸放大按钮并将取景器放在上面时,地图会一直放大,直到我们移开手指为止。

我尝试使用 setOnLongClickListener 和 setOnTouchListener 进行操作,搜索了 stackoverflow 论坛但找不到帮助。

这是我的 Android Activity 的完整 Java 代码。

public class MainActivity extends Activity {

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

    final TextView tv_1= (TextView) findViewById(R.id.tv_1);
    Button btn_1= (Button) findViewById(R.id.btn_1);
    Button btn_2= (Button) findViewById(R.id.btn_2);

    final TextView tv_2= (TextView) findViewById(R.id.tv_2);
    Button btn_3= (Button) findViewById(R.id.btn_3);
    Button btn_4= (Button) findViewById(R.id.btn_4);

    final TextView tv_3= (TextView) findViewById(R.id.tv_3);
    Button btn_5= (Button) findViewById(R.id.btn_5);
    Button btn_6= (Button) findViewById(R.id.btn_6);

    Button btn_7= (Button) findViewById(R.id.btn_7);


    btn_1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
        String count=tv_1.getText().toString();
        int cnt= Integer.parseInt(count);
        cnt++;
        tv_1.setText(""+cnt);
        }
    });
    btn_2.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
        String count=tv_1.getText().toString();
        int cnt= Integer.parseInt(count);
        cnt--;
        tv_1.setText(""+cnt);
        }
    });



    btn_3.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
        String count=tv_2.getText().toString();
        int cnt= Integer.parseInt(count);
        cnt++;
        tv_2.setText(""+cnt);
        }
    });
    btn_4.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
        String count=tv_2.getText().toString();
        int cnt= Integer.parseInt(count);
        cnt--;
        tv_2.setText(""+cnt);
        }
    });


    btn_5.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
        String count=tv_3.getText().toString();
        int cnt= Integer.parseInt(count);
        cnt++;
        tv_3.setText(""+cnt);
        }
    });
    btn_6.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
        String count=tv_3.getText().toString();
        int cnt= Integer.parseInt(count);
        cnt--;
        tv_3.setText(""+cnt);
        }
    });




    btn_7.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            tv_1.setText(""+0);
            tv_2.setText(""+0);
            tv_3.setText(""+0); 
        }
    });


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

【问题讨论】:

    标签: java android widget counter


    【解决方案1】:

    我建议你使用OnTouchListener。我们调用新的线程,直到你按下while 循环中的按钮:

    public class MainActivity extends Activity implements OnTouchListener {
    
    ...
    btn_1.setOnTouchListener(this);
    ...
    
    @Override
    public boolean onTouch(View arg0, MotionEvent arg1) {
        if (arg1.getAction()==MotionEvent.ACTION_DOWN){
            
            doPressDown();          
        }
        else if (arg1.getAction()==MotionEvent.ACTION_UP){
            
            doPressRelease();
        }
    
        return true;
    }  
    
    private void doPressDown() {
        isPressed = true;
        new Thread(() -> {
            while (isPressed == true) {
                // here we increment till user release the button
            }
        }).start();
    }
    
    private void doPressRelease(){
        isPressed = false;
    }
    

    }

    【讨论】:

      猜你喜欢
      • 2011-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多