繁体   English   中英

获取EditText背景资源

[英]Get EditText Background Resource

如何在Android活动中获取EditText背景资源?

我简短的示例应用程序是这样的:

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();
}

创建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
}

尝试这个,

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();
}

我的第一个想法是editTextState 像这样

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();
}

您可以根据在xml布局上设置的editText背景颜色定义状态值。 例如,如果您将editText设置为红色背景:

int editTextState = 1;

暂无
暂无

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

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