【问题标题】:How to highlight only one button in android如何在android中仅突出显示一个按钮
【发布时间】:2012-01-27 17:22:32
【问题描述】:

在我基于数据库值的项目中,我正在动态创建按钮。现在通过按下一个按钮,我突出显示该按钮。假设如果我按下另一个按钮,则该按钮将突出显示,第一个按下的按钮将不突出显示.

我的问题是:总是按下的按钮会突出显示,直到按下另一个按钮。

    for (int i = 0; i < arr.length; i++) {

        final Button b = new Button(this);          
        b.setWidth(80);
        b.setId(i + 1);         
        b.setText("M" + arr[i]);            
        ll.addView(b);      

        b.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                  b.setBackgroundColor(R.drawable.buttonbtn);

                  String str = b.getText().toString().substring(1);
                  Constant.diametervalue = str;
                  f1 = l.indexOf(Constant.diametervalue);
                  f2 = l.size();Log.e("","f1 value--"+f2);
                  Constant.length = f2;
                  InnerList adapter11 = new InnerList(this, str); 
                  adapter11.getCount();
                  gallery.setAdapter(adapter11);    
        }
    }

我的代码通过按一个按钮突出显示。稍后如果我按第二个按钮第一个和第二个按钮突出显示。

【问题讨论】:

  • 如何突出显示按钮?

标签: android


【解决方案1】:

仅具有按下状态的按钮元素。在复选框中,有一种状态称为状态检查。更好的是,您可以创建一个带有按钮图像的复选框。

【讨论】:

    【解决方案2】:

    试试这个:

    假设有两个按钮 Button1 和 Button2。

    并为 2 个按钮创建四个可绘制对象:

    示例:普通图片:buttonone_normal、buttontwo_normal。

    选中的图片:buttonone_selected,buttontwo_selected

     and onClickListeners put something like this:
    buttonone.setOnClickListener(new OnClickListener(){
    @Override
    public void onClick(View v) 
    {
      buttonone.setImageResource(R.drawable.buttonone_selected)
      buttontwo.setImageResource(R.drawable.buttontwo_normal)
    }
    buttontwo.setOnClickListener(new OnClickListener(){
    @Override
    public void onClick(View v) 
    {
      buttonone.setImageResource(R.drawable.buttonone_normal)
      buttontwo.setImageResource(R.drawable.buttontwo_selected)
    }
    

    【讨论】:

    • 我在这里动态创建按钮。如何编写单独的 onclicklistener 方法。 for (int i = 0; i
    • 如果已经在 xml 文件中创建了按钮,那么你的代码将 aplly.Plz 一旦检查并告诉解决方案
    • 每个按钮都有不同的 id,对吧?然后用那个 id 处理
    【解决方案3】:

    我认为您需要将复选框与组而非普通按钮视图一起使用。如果不清楚,我可以为您编写一些代码.. 但基本上您将使用复选框并更改样式以同意您要显示的内容.. 在这种情况下,您将能够突出显示(检查)并单击其他button(checkbox) 如果你在一个组中使用所有它们,第一个检查将消失

    =======添加代码=======

    我的朋友来了:

    关于主要活动:

    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.CheckBox;
    import android.widget.CompoundButton;
    import android.widget.CompoundButton.OnCheckedChangeListener;
    
    public class HighListButtonActivity extends Activity implements OnCheckedChangeListener {
        private CheckBox b1,b2,b3;
        private int selectedChId=-1;
    
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            b1=(CheckBox)findViewById(R.id.ck1);
            b2=(CheckBox)findViewById(R.id.ck2);
            b3=(CheckBox)findViewById(R.id.ck3);
    
            b1.setOnCheckedChangeListener(this);
            b2.setOnCheckedChangeListener(this);
            b3.setOnCheckedChangeListener(this);
        }
    
      @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        // here just to follow the checkbox which is checked to uncheck it later when other checkbox is checked..
          if(selectedChId==-1){
              selectedChId=buttonView.getId();
          }else if(selectedChId==buttonView.getId()){
              selectedChId=-1;
          }else{
              ((CheckBox)findViewById(selectedChId)).setChecked(false);
              selectedChId=buttonView.getId();
          }
    
          //make your logic here......
    
    }
    
    
    }
    

    main.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    
        <CheckBox 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="bt1"
            android:button="@drawable/hlor"
            android:background="@drawable/hlor"
            android:id="@+id/ck1"        
            />
        <CheckBox 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="bt2"
            android:button="@drawable/hlor"
            android:background="@drawable/hlor"
            android:id="@+id/ck2"        
            />
        <CheckBox 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="bt3"
            android:button="@drawable/hlor"
            android:background="@drawable/hlor"
            android:id="@+id/ck3"        
            />
    
    </LinearLayout>
    

    hlor.xml(作为选择器样式):

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_checked="true" android:drawable="@drawable/hlord" />
        <item android:state_checked="false" android:drawable="@drawable/hlornd" />
    </selector>
    

    hlord.xml(作为检查样式drawable):

    <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
    <shape>
      <android:solid android:color="#FF555555"/>
    </shape>
    </item>
    </layer-list>
    

    hlornd.xml(作为未选中样式drawable):

    <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
    <shape>
      <android:solid android:color="#FF000000"/>
    </shape>
    </item>
    </layer-list>
    

    为了突出显示只需更改 hlornd/hlord 样式的颜色

    祝你好运……

    【讨论】:

    • 对不起,我没有得到你,你能解释一下。我不能使用复选框
    • 在放置数据库值的每个按钮上。因此,按下该按钮,其值将显示在屏幕上。所以只有那个按钮应该突出显示
    • for(int i=0;i
    猜你喜欢
    • 1970-01-01
    • 2011-07-04
    • 2013-03-26
    • 2010-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-24
    相关资源
    最近更新 更多