Y.Gökalp Y.Gökalp - 26 days ago 6
Java Question

Assign a variable in EditText

I have a project in android studio.I want to assign a variable in EditText.And I want to print the screen on a condition when clicking on the button

P.S : sorry for my bad english I translation google

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main1);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});

final Button btn_baslat =(Button)findViewById(R.id.btn_onayla);
final TextView txt_yazi =(TextView)findViewById(R.id.txt_hazir);
final EditText editxt_deger =(EditText)findViewById(R.id.et_trojanyaz);
final String editxt_okuma = editxt_deger.getText().toString();

btn_baslat.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(editxt_okuma.equalsIgnoreCase("myalfa")){
txt_yazi.setText("BAŞARDIN !");
}
else{
txt_yazi.setText("BAŞARAMADIN !");
}
}
});

}


In this code when I write EditText "myalfa" it's not show "BAŞARDIN !"

Answer

You need to compare the EditText value rather can comparing the actual EditText itself.
Try this inside onClick method,

btn_baslat.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View view) {
        String text = editxt_okuma.getText().toString();
        if(text.equalsIgnoreCase("myalfa")){
            txt_yazi.setText("BAŞARDIN !");
        } else {
            txt_yazi.setText("BAŞARAMADIN !");
        }
    }
});
Comments