我有两个按钮:1和2.按钮1用于将数字从0增加到15,按钮2用于减少,输入是数字的当前值为0.
假设数字的初始值为= -1。然后我按下按钮1,数字将增加到1值(0)。如果我按5次,则当前数值为4.然后我按下按钮2,数值将从5
减少For example,
Button 1 press Button 2 press
0
1
2
3
4
4
3
2
...
如果该值小于0,则禁用设置按钮2,如果该值大于15,则禁用设置按钮1。我使用下面的代码,但它不能很好地工作。你能告诉我发生了什么吗?
int number=-1;
public void onClickBtn1 (View v)
{
number++;
btn2.setEnabled(true);
if(number>=16) {
number = 16;
btn1.setEnabled(false);
}
if(number<=15) {
Log.d("TAG", number);
}
}
public void onClickBtn2 (View v)
{
btnTop.setEnabled(true);
number--;
if(number<=-1) {
number =-1;
btn2.setEnabled(false);
}
if(number>=0) {
Log.d("TAG",number);
}
}
更新:现在,按下按钮2时,我可以打印为0到5(按下按钮1)和(5到0)但是代码看起来很复杂,按钮禁用不起作用该数字超过15(&lt; = 16),并且小于0(&gt; = - 1)
我的代码是
int number=-1;
public void onClickBtn1 (View v)
{
btn2.setEnabled(true);
if(number<=-1) {
number =-1;
}
number++;
if(number>=16) {
number = 16;
btn1.setEnabled(false);
}
if(number<=15) {
Log.d("TAG",number);
}
}
public void onClickBtn2 (View v)
{
btn1.setEnabled(true);
if(number<=-1) {
number =-1;
btn2.setEnabled(false);
}
if(number>=16) {
number = 15;
btn1.setEnabled(false);
}
if(number>=0) {
Log.d("TAG",number);
}
number--;
}
答案 0 :(得分:2)
这是一个简单的逻辑。
it
它应该足以解决您的问题。 如果有任何问题,请告诉我。
答案 1 :(得分:1)
我认为这是你想要实现的目标。试试这个。
public void onClickBtn1 (View v)
{
number++;
btn2.setEnabled(true);
if(number==15) {
btn1.setEnabled(false);
}
Log.d("TAG", number);
}
public void onClickBtn2 (View v)
{
btn1.setEnabled(true);
number--;
if(number==0) {
btn2.setEnabled(false);
}
Log.d("TAG",number);
}
答案 2 :(得分:0)
据我所知,您的问题可能就是解决方案
全局变量
public final String BUTTON1 = "button1", BUTTON2 = "button2";
TextView button2;
int number = 0;
String lastButtonClicked = "button1";
和您的buttonClickListeners
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
button2.setEnabled(true);
if (lastButtonClicked.equals(BUTTON1) && number < 15)
number++;
lastButtonClicked = BUTTON1;
if (number == 15) {
button1.setEnabled(false);
}
Log.d("TAG", "" + number);
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
button1.setEnabled(true);
if (lastButtonClicked.equals(BUTTON2) && number > 0)
number--;
lastButtonClicked = BUTTON2;
if (number == 0) {
button2.setEnabled(false);
}
Log.d("TAG", "" + number);
}
});
button2.setEnabled(false);
Log.d("TAG", "" + 0);
答案 3 :(得分:0)
Button button1, button2;
int number = -1, initial = -1;
button2.setEnabled(false); // initially setting button2 disable
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(number < 15) {
number++;
}
//this is because: If the value is smaller than 0, then set button 2 is disabled -> enabling if > 0
if(number > 0) {
button2.setEnabled(true);
}
if(number == 15){
button1.setEnabled(false); //because of range: from 0 to 15
}
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
button1.setEnabled(true);
// below condition is for: (Then I press the button 1, the number will increase to 1 value (0). If I press 5 times, the current number value is 4. Then I press the button 2, the values will decrease from 5)
//and initial value is -1 for once, as I think you didn't need to check it later because range is 0-15 but initially you are taking -1.
if(initial == -1){
number = number - initial;
initial = 0; // changed the value
}
if(number > 0) {
number--;
}
//as your range is 0-15, so can't be less than 0
if(number == 0){
button2.setEnabled(false);
}
}
});
答案 4 :(得分:0)
public class MainActivity extends AppCompatActivity {
int number = -1;
int total;
Button plus, minus;
TextView textViewTotal;
private static String TAG="MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
plus = (Button) findViewById(R.id.btn_plus);
minus = (Button) findViewById(R.id.btn_minus);
textViewTotal = (TextView) findViewById(R.id.total);
plus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
total = number;
if(number == 15) {
plus.setEnabled(false);
} else {
number++;
plus.setEnabled(true);
textViewTotal.setText(String.valueOf(number));
}
if (number > 0) {
minus.setEnabled(true);
}
}
});
minus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
total = number;
if(number < -1) {
minus.setEnabled(false);
plus.setEnabled(true);
} else {
minus.setEnabled(true);
plus.setEnabled(true);
number --;
textViewTotal.setText(String.valueOf(number+1));
if (total == number) {
number--;
textViewTotal.setText(String.valueOf(number));
}
}
}
});
}
}