gpt4 book ai didi

java - Button.setEnabled 在需要时不启用按钮

转载 作者:行者123 更新时间:2023-11-30 10:46:10 26 4
gpt4 key购买 nike

所以我想要两个按钮,一个用于在 TextView 上加 1,另一个用于减去。

textview 上的结果不能小于 0。

所以我想在文本为 0 时将其设置为禁用,这样它就不能变成负值。但这并不像我想的那样工作,当我按下加号按钮并添加 1 然后按下减号按钮减去 1(textview 上的结果现在为 0),然后再次使用加号按钮添加一个然后减去,减号按钮是不工作,它没有启用。

这是我的代码

int rezultat;
rezultat = Integer.parseInt(brojIntervala.getText().toString());

if (rezultat == 0){
homeScreenMinus.setEnabled(false);
brojIntervala.setText("0");
} else if (rezultat > 0){
homeScreenMinus.setEnabled(true);
}
}
});



package hiitbydario.com.daroioradecic.hiittraining;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

public class HomeScreen extends AppCompatActivity {
Button homeScreenPlus, homeScreenMinus;
TextView brojIntervala;
ImageButton homeScreenStart;
int counter = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_screen);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

homeScreenPlus = (Button) findViewById(R.id.homeScreenPlus);
homeScreenMinus = (Button) findViewById(R.id.homeScreenMinus);
homeScreenStart = (ImageButton) findViewById(R.id.homeScreenStart);

brojIntervala = (TextView) findViewById(R.id.brojIntervala);

homeScreenPlus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dodajInterval();
}
});

homeScreenMinus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
oduzmiInterval();
int rezultat;
rezultat = Integer.parseInt(brojIntervala.getText().toString());

if (rezultat == 0){
homeScreenMinus.setClickable(false);
brojIntervala.setText("0");
} else if (rezultat > 0){
homeScreenMinus.setClickable(true);
}
}
});


}

private void oduzmiInterval() {
counter--;
brojIntervala.setText(Integer.toString(counter));
}

private void dodajInterval() {
counter++;
brojIntervala.setText(Integer.toString(counter));
}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}

最佳答案

我建议继续使用 setEnabled() 而不是 setClickable(),这样用户也可以看到按钮已禁用。

问题是您只会在单击减号按钮时更新它的状态。但是,您也需要在单击加号按钮时更新它,以便在从 0 转换为正值时重新启用它。

homeScreenPlus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dodajInterval();
homeScreenMinus.setEnabled(counter > 0);
}
});

关于java - Button.setEnabled 在需要时不启用按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36696370/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com