2016-02-22 41 views
0

我正在尝试为学校的项目创建一个最大卧推按钮计算器。我对编码非常陌生,无法弄清楚为什么这种方法不起作用。加号和菜单权重按钮从不起作用。我以前使用OnClickListener和OnLongClickListener工作。只要按住按钮,我只想让OnTouchListeners保持增加重量。按钮上的多个OnTouchListeners以及Android Studio中的OnClickListeners按钮

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.TextView; 



public class MainActivity extends AppCompatActivity implements OnClickListener { 

private Button plusreps; 
private Button menusreps; 
private Button Calculate; 
private Button plus; 
private Button menus; 
private TextView textView; 
private TextView textView2; 
private TextView textView3; 
int reps; 
int Maxint; 
double Max; 
double weight; 



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

    textView = (TextView) findViewById(R.id.textView); 
    textView2 = (TextView) findViewById(R.id.textView2); 
    textView3 = (TextView) findViewById(R.id.textView3); 

    plusreps = (Button) findViewById(R.id.button5); 
    menusreps = (Button) findViewById(R.id.button4); 
    menus = (Button) findViewById(R.id.button3); 
    plus = (Button) findViewById(R.id.button2); 
    Calculate = (Button) findViewById(R.id.button); 

    Calculate.setOnClickListener(this); 

    reps = 0; 
    weight = 100; 
    textView.setText("" + String.valueOf(weight)); 
    textView2.setText("" + String.valueOf(reps)); 

    plus.setOnTouchListener(new View.OnTouchListener() { 
     @Override 
     public boolean onTouch(View view, MotionEvent mEvent1) { 
      if (mEvent1.getAction() == MotionEvent.ACTION_DOWN) 
       startIncreasingWeight(); 
      else if (mEvent1.getAction() == MotionEvent.ACTION_UP) 
       stopIncreasingWeight(); 
      return false; 
     } 

     private void stopIncreasingWeight() { 
      textView.setText("" + String.valueOf(weight)); 
     } 

     private void startIncreasingWeight() { 
      weight = weight++; 
      textView.setText("" + String.valueOf(weight)); 

     } 
    }); 


    menus.setOnTouchListener(new View.OnTouchListener() { 
     @Override 
     public boolean onTouch(View view, MotionEvent mEvent) { 
      if (mEvent.getAction() == MotionEvent.ACTION_DOWN) 
       startDecreasingWeight(); 
      else if (mEvent.getAction() == MotionEvent.ACTION_UP) 
       stopDecreasingWeight(); 
      return false; 
     } 

     private void stopDecreasingWeight() { 
      textView.setText("" + String.valueOf(weight)); 
     } 

     private void startDecreasingWeight() { 
      weight = weight--; 
      textView.setText("" + String.valueOf(weight)); 

     } 
    }); 
} 



public void onClick(View v) { 
    switch (v.getId()) { 
    case R.id.button4: 
     if (reps > 1){ 
      textView2.setText("" + String.valueOf(reps - 1)); 
      reps = reps - 1; 
     } 
     else { 
      textView2.setText("" + String.valueOf(reps)); 
     } 
     break; 
    case R.id.button5: 
     textView2.setText("" + String.valueOf(reps + 1)); 
     reps = reps + 1; 
     break; 
    case R.id.button: 
     Max = reps * weight * 0.0333 + weight; 
     int Maxint = (int) Math.round(Max); 
     textView3.setText("Your max is: " + String.valueOf(Maxint)); 
     break; 

} 





}} 
+0

onTouch没有比的onClick不同的只是它提供了更多的行动,比如ACTION_UP,ACTION_DOWN,ACTION_MOVE。我想说的是,MotionEvent.Action_Down中的代码仍然只执行一次。 –

+1

查看此[问题](http://stackoverflow.com/questions/10511423/android-repeat-action-on-pressing-and-holding-a-button)为您的问题 –

回答

1

把这个:

plusreps.setOnClickListener(this); 
menusreps.setOnClickListener(this);