Press the + button to go up 3000 - press the button to go down It's a program where the total amount is calculated according to the button pressed It's a hassle to press the button, so I want to make it count even if I type a number in the edit text right away For example, if you hit 10, the total amount is calculated as 30,000 won.
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
Button bh1, bh2, bca;
TextView tr;
EditText th;
String sh;
int nh;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bh1 = (Button)findViewById(R.id.button);
bh2 = (Button)findViewById(R.id.button2);
bca = (Button)findViewById(R.id.button3);
th=(EditText)findViewById(R.id.editText);
tr=(TextView)findViewById(R.id.textView);
nh = 0;
bh1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
nh = nh + 1;
th.setText(nh+"");
}
});
bh2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
nh = nh - 1;
th.setText(nh+"");
}
});
bca.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tr.setText ("Total amount:"+(nh*3000)+"circle");
}
});
}
}
I did it like this, but the buttons are calculated, but if you type numbers in EightText, you can'tcrying Please help meㅠ<
android-studio edittext
Just as you added a responding listener when you press the button, I think you can add a responding listener to the edit text every time the text changes.
th.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// When there is a change in the text being entered
nh = parseInt(s.toString());
tr.setText ("Total amount:"+(nh*3000)+"circle");
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
@Override
public void afterTextChanged(Editable arg0) { }
});
int parseInt(String numberString) {
try {
return Integer.parseInt(numberString);
} } catch (NumberFormatException nfe) {
return 0;
}
}
© 2024 OneMinuteCode. All rights reserved.