![](/img/trans.png)
[英]How to change the color of just ONE button when the button is clicked from a container with 9 buttons Flutter
[英]How to make clicked button highlighted with one color and set other all buttons with one color when button is cliked
实际上,当我单击按钮时,我将有多个矩形按钮,我将获得列表视图。现在,当我单击按钮时,我需要用红色突出显示的颜色是什么问题,而其他所有按钮必须为绿色。 请帮助我。
活动:
if(name.isEmpty()||name==null){
billnumber +=name;
}
else{
if(count==3){
billnumber +=" , "+name;
}
else if(count==2){
billnumber +=" , "+name;
}
else if(count==1){
billnumber +=name;
}
}
if (count == 3) {
id++;
final Button dynamicTextView = new Button(this);
dynamicTextView.setLayoutParams(new LayoutParams(350,
LayoutParams.MATCH_PARENT));
dynamicTextView.setBackgroundColor(Color.parseColor("#1c7900"));
dynamicTextView.setText("Bill Numbers\n"+billnumber);
dynamicTextView.setId(id);
final Button dynamicinvnumber=new Button(this);
if(id==(clickedid-1)){
dynamicTextView.setBackgroundColor(Color
.parseColor("#cf0000"));
}
if(istouched){
if (id == clickedid) {
touchedlist=dynamicTextView.getText().toString();
if(touchedlist.contains("Bill Numbers"))
{
touchedlist=touchedlist.replace("Bill Numbers","");
}
text=touchedlist;
if(bumpedbillnumber!="0")
{
if(touchedlist.contains(bumpedbillnumber))
{
touchedlist=touchedlist.replace(bumpedbillnumber,"");
}
if(text.contains(bumpedbillnumber))
{
text=text.replace(bumpedbillnumber,"");
}
}
dynamicTextView.setBackgroundColor(Color
.parseColor("#cf0000"));
}
}
if(refreshlist!=1)
{
if(id==1)
{
dynamicTextView.setBackgroundColor(Color
.parseColor("#cf0000"));
}
}
if (invoice1 == null) {
if (id ==1) {
dynamicTextView.setBackgroundColor(Color
.parseColor("#cf0000"));
}
}
dynamicTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 30);
dynamicTextView.setOnClickListener(new OnClickListener() {
@SuppressWarnings("deprecation")
@Override
public void onClick(View v)
{
EnabledButton = dynamicTextView.getId();
clickedid=dynamicTextView.getId();
dynamicTextView.setBackgroundColor(Color
.parseColor("#cf0000"));
dynamicTextView.setSelected(true);
invoiceToDisplay = null;
invoiceToDisplay = new ArrayList<String>();
text = dynamicTextView.getText().toString();
if(text.contains("Bill Numbers"))
{
text=text.replace("Bill Numbers","");
}
String s[] = text.split(" , ");
invoice = text.split(" , ");
System.out.println("s" + s[0]);
istouched=true;
refreshlist=1;
if (s.length == 1) {
if(s[0].contains("\n"))
{
s[0]=s[0].replace("\n","");
}
int invoice11=receiptlist.indexOf(s[0].trim());
String invoiceselected=invoiceList.get(invoice11);
tv1.setVisibility(View.GONE);
tv2.setVisibility(View.GONE);
tv.setVisibility(View.VISIBLE);
footerText3.setVisibility(View.GONE);
footerText2.setVisibility(View.GONE);
loadListViews(invoiceselected, listView1, headerButton
Drawable Xml:
<corners android:radius="3dp" />
<solid android:color="#124a01" />
<stroke
android:width="2px"
android:color="#c8ea32" />
btn_selected.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="red_color"/>
</shape>
btn_un_selected.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="ordinary_color"/>
</shape>
btn_selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/btn_selected" android:state_selected="true"></item>
<item android:drawable="@drawable/btn_selected" android:state_pressed="true"/>
<item android:drawable="@drawable/btn_un_selected"/>
</selector>
在xml中更改按钮的按钮背景
<Button
........
android:background="@drawable/btn_selector/>
在代码更改
btn.setSelected(!btn.isSelected);
无需以编程方式创建按钮,而是在xml上创建它们,然后在您的活动中检索其ID,以便您可以访问它们; 在为他们设置set onTouchListener之后,并在其中进行设置。
将Rect rect和shouldPerformClick定义为活动的全局变量。
button1.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getActionMasked()){
case MotionEvent.ACTION_DOWN:
changeButtonBackgrounds(v.getId());
shouldPerformClick = true;
break;
case MotionEvent.ACTION_MOVE:
rect = new Rect();
v.getDrawingRect(rect);
if(!(rect.left < event.getX() && event.getX() < rect.right)){
shouldPerformClick = false;
resetButtonBackgrounds();
}
if(!(rect.bottom < event.getY() && event.getY() < rect.top)){
shouldPerformClick = false;
resetButtonBackgrounds();
}
break;
case MotionEvent.ACTION_UP:
resetButtonBackgrounds();
if(shouldPerformClick){
button1.performClick;
}
break;
}
return true;
}
});
我会在changeButtonBackgrounds()上使用switch(viewID),我认为我的整个方法需要更多样板,但它更理想。
定义按钮数组buttons
,这些按钮是XML文件中的按钮,并且是一个全局变量int selectedIndex=0
。
确保在XML文件中,所有按钮均为绿色。
selectedIndex
将是最后一个选定按钮的数组中的索引。
然后,在数组中的每个按钮上
for (int i = 0 ; i < buttons.length ; i++)
buttons[i].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
buttons[selectedIndex]
.setBackgroundTintList(ColorStateList.valueOf(getResources()
.getColor(R.color.green, getTheme())));
switch (view.getId()) {
case R.id.button1:
buttons[0].setBackgroundTintList(ColorStateList.valueOf(getResources()
.getColor(R.color.red)));
selectedIndex = 0;
break;
case R.id.button2:
buttons[1].setBackgroundTintList(ColorStateList.valueOf(getResources()
.getColor(R.color.red)));
selectedIndex = 1;
break;
.
.
.
}
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.