gpt4 book ai didi

java - 在 EditText 内写入

转载 作者:行者123 更新时间:2023-12-01 19:35:01 25 4
gpt4 key购买 nike

我有两个 EdiText,当我在第一个 EdiText 中写入数字时,我需要将第二个 EdiText 设置为 162 - 第一个。如果需要重新输入第二个数字,组件应重新计算第一个数字。

如果我在第二个中写一些东西,第一个的行为必须与第二个完全相同。

下面是我的代码,但它不起作用:

    inputScoreWe = findViewById(R.id.inputScoreWe);
inputScoreYou = findViewById(R.id.inputScoreYou);

View.OnClickListener inputScoreListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
try {

int inputScoreWeInteger = Integer.parseInt(inputScoreWe.getText().toString());
int inputScoreYouInteger = Integer.parseInt(inputScoreYou.getText().toString());

if (inputScoreWeInteger > 0) {
inputScoreYouInteger = 162 - inputScoreWeInteger;
} else if (inputScoreYouInteger > 0) {
inputScoreWeInteger = 162 - inputScoreYouInteger;
}

String s1 = inputScoreWeInteger + "";
String s2 = inputScoreYouInteger + "";

inputScoreWe.setText(s1);
inputScoreYou.setText(s2);

} catch (Exception e) {
Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_LONG).show();

}
}
};

inputScoreWe.setOnClickListener(inputScoreListener);
inputScoreYou.setOnClickListener(inputScoreListener);

最佳答案

使用文本更改监听器在每次更改值时触发

 inputScoreWe.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(s.length() > 0){
inputScoreYou.setText(162 - Integer.parseInt(inputScoreYou.getText().toString())+"");
}

}

@Override
public void afterTextChanged(Editable s) {

}
});

关于java - 在 EditText 内写入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58166595/

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