I want when i write 5 in the EditText then i click the button i want the TextView to change to 5, then if i write 6 and click again on the button i want the TextView to change to 11...
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="how much do you want to add ?"
android:id="@+id/editText" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="0"
android:textSize="50sp"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginTop="86dp"
android:id="@+id/TextView" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/add"
android:onClick="add"
android:layout_below="@id/editText"
android:hint="add"/>
You can check the following code snippet. Make sure to add conditions/check that input is always in numeric:
yourbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(yourtxtview.getText().toString().trim().length()>0 ){
if(youredtText.getText().toString().trim().length()>0){
yourtxtview.setText(String.valueOf(Integer.valueOf(yourtxtview.getText().toString())
+Integer.valueOf(youredtText.getText().toString())));
}else{
yourtxtview.setText(String.valueOf(Integer.valueOf(yourtxtview.getText().toString())+
0));
}
}else if(youredtText.getText().toString().trim().length()>0){
yourtxtview.setText(String.valueOf(Integer.valueOf(youredtText.getText().toString())+
0));
}
}
});
TextView textView;
EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
editText = findViewById(R.id.editText);
textView.setText("0");
}
public void add(View v) {
int x = Integer.parseInt(textView.getText().toString());
textView.setText(String.valueOf(x + Integer.parseInt(editText.getText().toString())));
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.