简体   繁体   English

获取EditText背景资源

[英]Get EditText Background Resource

How can I get my EditText Background Resource in my Android Activity? 如何在Android活动中获取EditText背景资源?

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中插入背景时只需对其进行映射

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 . 我的第一个想法是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. 您可以根据在xml布局上设置的editText背景颜色定义状态值。 For example if you set the editText with red background: 例如,如果您将editText设置为红色背景:

int editTextState = 1;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM