p.karimdost p.karimdost - 4 months ago 38
Java Question

Set Text From EditText to TextView Using If-Else

I want that if I input a in edittext, text will display string a1 and if I input b in edittext, text will display string b1 and if I input ab, then textView display both string a1 and b1.

public class MainActivity extends Activity {
Button btn;
EditText et;
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn =(Button)findViewById(R.id.button);
et =(EditText)findViewById(R.id.editText);
tv =(TextView)findViewById(R.id.textView);

btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name =et.getText().toString();
String a = "a";
String a1 = "\u00E1";
String b = "b";
String b1 = "\u00DF";

if(name.equals(a)){
tv.setText(a1);
}
else if (name.equals(b)){
tv.setText(b1);
}
}
});

}}

ddb ddb
Answer

you should use replaceAll function as below in the onClickListener instead of the if/else branch

name = name.replaceAll(a,a1);
name = name.replaceAll(b,b1);
tv.setText(name);