[英]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.