How can I get my EditText Background Resource in my Android Activity?
My short Example App is this:
Button1: EditText.setBackgroundResource(R.drawable.redstyle); //Red style xml
Button2: EditText.setBackgroundResource(R.drawable.greenstyle); // Green style xml
if(????????????????????????????????????){
Toast.makeText(this,"Green is selected",Toast.LENGTH_LONG).show();
}else{
Toast.makeText(this,"Red is selected",Toast.LENGTH_LONG).show();
}
What you can do it to create an HashMap, and when you insert a background in a EditText just map it
HashMap<EditText,Integer> hashMap;
edit_text.setBackgroundResources(R.drawable.redstyle);
hashMap.put(edit_text,R.drawable.redstyle);
if(hashMap.get(edit_text)==R.drawable.redstyle)
{
//do something
}else{
//do something
}
try this,
if(edit_text.getBackground()==getResources().getDrawable(R.drawable.redstyle)){
Toast.makeText(MainActivity.this,"red selected",Toast.LENGTH_LONG).show();
}else{
Toast.makeText(MainActivity.this,"Green selected",Toast.LENGTH_LONG).show();
}
My first thought was a editTextState
. Something like this
Button1: EditText.setBackgroundResource(R.drawable.redstyle); //Red style xml
editTextState = 1;
Button2: EditText.setBackgroundResource(R.drawable.greenstyle); // Green style xml
editTextState = 2;
if(editTextState == 2){
Toast.makeText(this,"Green is selected",Toast.LENGTH_LONG).show();
}else{
Toast.makeText(this,"Red is selected",Toast.LENGTH_LONG).show();
}
You can define the state value according the editText background color you have set on the xml layout. For example if you set the editText with red background:
int editTextState = 1;
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.