Issue
I have a text box (EditText) where I will insert text, and a button that should calculate the lentgh of that text and display it in the another text box. How should I code that button?
Solution
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b1=findViewById(R.id.b1);
Button b2=findViewById(R.id.b2);
EditText t1=findViewById(R.id.t1);
TextView t2;
t2 = findViewById(R.id.t2);
b1.setOnClickListener(view -> {
int nb=0;
for(int i=0; i<t1.length(); ++i){
nb++;
}
t2.setText(Integer.toString(nb));
});
b2.setOnClickListener(view -> {
t2.setText(t1.getText().toString());
});
}
Answered By - coderGirl
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.