我正在尝试为学校项目创建一个最大卧推计算器。我对编码很新,无法弄清楚为什么这不起作用。加号和菜单重量按钮永远不会起作用。之前我确实使用过OnClickListener和OnLongClickListener。我只是希望OnTouchListeners在按下按钮的同时不断增加重量。
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 :(得分:1)
把这个:
plusreps.setOnClickListener(this);
menusreps.setOnClickListener(this);