【问题标题】:Android Set button color on first click, unset on second clickAndroid 在第一次点击时设置按钮颜色,在第二次点击时取消设置
【发布时间】:2014-11-09 05:11:23
【问题描述】:

请我在这方面需要帮助。 我在这里搜索过,但我看到的答案对我不起作用,帖子很旧,功能大多已弃用。

我试图通过单击设置按钮的颜色以突出显示它们并在第二次单击时取消设置颜色。这就像从多个按钮中做出选择一样,如果我再次单击选定的按钮,可能在改变了我的选择之后,颜色应该恢复为默认值。这样我就只剩下突出显示的选定按钮了。 这些按钮是使用 gridview 中的适配器生成的,并且 onclicklistener 适用于所有这些按钮。 我使用的代码如图所示:

public class ButtonAdapter extends BaseAdapter {  
         private Context context;            

         public View getView(int position, View convertView, ViewGroup parent) 

         {            
             final Button btn;  
          if (convertView == null) {    

           btn = new Button(context);  
           btn.setLayoutParams(new GridView.LayoutParams(40, 40));  
           btn.setPadding(2, 2, 2, 2);  
           }   
          else {  
           btn = (Button) convertView;  
          }  
          //exus

          btn.setText(Integer.toString(gridNumbers[position]));   

          btn.setTextColor(Color.BLACK);               
          btn.setId(position);
          btn.setOnClickListener(new View.OnClickListener() {


            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                //btn.setBackgroundColor(Color.GREEN);
                //Toast.makeText(getBaseContext(),  "Button clicked", Toast.LENGTH_LONG).show();

                if (v.getSolidColor()!=Color.GREEN)
                {
                    btn.setBackgroundColor(Color.GREEN);            

                }
                else
                {
                    btn.setBackgroundColor(Color.GRAY);
                }

            }
        });

          return btn;  
         }  
        }
}

我的 XML:

<GridView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/gridview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:numColumns="8"
        android:columnWidth="20dp"          
        android:stretchMode="columnWidth"           
        android:gravity="center" />

【问题讨论】:

标签: android button colors


【解决方案1】:

您可以使用布尔属性列表而不是这样做。 在你的类中设置一个公共布尔列表(它应该是公共的并且在任何函数之外,否则 onclicklistener 会出错)

List<boolean> blist=new Arraylist<boolean>(Size);

//Size is maximum number of buttons

int index;

然后每当你创建一个新按钮时添加这个:

blist.add(index,false);
index++;

在 onclicklistener 中;从按钮的位置找到按钮的索引,并将索引保​​存在一个名为 pos 的整数中。

if(blist.get(pos)==false)
{
 //not clicked yet
 blist.remove(pos);
blist.add(pos,true);
//here write the code u need for this if
}
else
{
blist.remove(pos);
blist.add(pos,false);
//todo: ur code for else
}

【讨论】:

  • 谢谢。我没试过,我会试试然后回复你
【解决方案2】:

我试过这种方式,它对我有用,如果你想在点击时改变

counter = 1;
        //By Default set color
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (counter == 1)
                {
                   // Default color
                    counter = 2;
                }
                else
                {
                    //your color
                    counter = 1;
                }
            }
        });

【讨论】:

    【解决方案3】:

    在 GridView 中重用的视图。因此,您应该在基本适配器中定义按钮的状态。

    获取一个 ArrayList 来保存您选择的索引,并在未选择网格时将其删除。

    例如:

    ArrayList<Integer> selectedItems;
    

    在构造函数中

    selectedItems = new ArrayList<Integer>();
    

    在 OnClickListener 中

    public void onClick(View v) {
    if (selectedItems.contains(new Integer(position))) {
         selectedItems.remove(new Integer(position));
         notifyDataSetChanged();    
    } else {
        selectedItems.add(new Integer(position));
        notifyDataSetChanged();
    }
    }
    

    在 getView() 中:

    if (selectedItems.contains(new Integer(position))) {
             btn.setBackgroundColor(Color.GREEN);    
        } else {
            btn.setBackgroundColor(Color.GRAY); 
        }
    

    【讨论】:

      【解决方案4】:

      使用toggle button insted 普通按钮。喜欢

      <ToggleButton 
              android:id="@+id/toggle"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:background="@drawable/check"   //check.xml
              android:layout_margin="10dp"
              android:textOn=""
              android:textOff=""
              android:focusable="false"
              android:focusableInTouchMode="false"
              android:layout_centerVertical="true"/>
      

      然后在drawable文件夹中创建一个xml文件check.xml

      <?xml version="1.0" encoding="utf-8"?>
      <selector xmlns:android="http://schemas.android.com/apk/res/android">
          <!-- When selected, use grey -->
          <item android:drawable="@drawable/selected_image"
                android:state_checked="true" />
          <!-- When not selected, use white-->
          <item android:drawable="@drawable/unselected_image"
              android:state_checked="false"/>
      
      </selector>
      

      refrence.

      【讨论】:

      • 感谢您的快速回复。但我使用的按钮不是使用 xml 布局生成的,而是使用 gridview 中的 arrayadapter 动态生成的
      • 在代码中使用 ToggleButton insted of Button 并为其动态设置属性.. 我自己没有尝试过,但它应该和普通 Button 一样好.. 参考 developer.android.com/reference/android/widget/…
      • 谢谢,我会尝试并回复您...尝试了 Chirag Jain 的建议,它有效。
      猜你喜欢
      • 1970-01-01
      • 2016-07-14
      • 1970-01-01
      • 1970-01-01
      • 2018-03-09
      • 2017-07-03
      • 1970-01-01
      • 1970-01-01
      • 2021-06-01
      相关资源
      最近更新 更多