动态按钮单击android

时间:2012-04-18 09:18:30

标签: android

我想在点击特定按钮时更改按钮的图像,并取消选择所有其他的请帮助。

这是我的代码

public void onClick(View view) {
    Object tag = ((Button) view).getTag();

    System.out.println("value o ftagagdsgsdg:::" +tag.toString());
    String strtag = tag.toString();
    System.out.println("value of strtag:::" +strtag);

    enablebutton = Integer.valueOf(tag.toString());

    System.out.println("value of enable buttons variable::" +enablebutton);
    DeselectButtons();

    ((Button)view).setEnabled(true);
    for(int a = 0;a<adapt_obj.city_id_array.length;a++){
        System.out.println("ddsgdgsdg::"+adapt_obj.city_code_array[a]);
        System.out.println("value o ftagagdsgsdg:inside for loop::" +tag.toString());

        if(tag.toString() == adapt_obj.city_code_array[a]){
            ((Button)view).setFocusable(true);
            //clicked = false;

            // Calling process to fetch the data of city 
            selectedcityidclicked(tag.toString(), view);
            ((Button)view).setSelected(true);

            /*Drawable dr = getResources().getDrawable(R.drawable.location_btn_active_1);// active
            Bitmap bitmap = ((BitmapDrawable) dr).getBitmap();
            // Scale it to 50 x 50
            // Set your new, scaled drawable "d"
            ((Button)view).setBackgroundDrawable(new BitmapDrawable(Bitmap.createScaledBitmap(bitmap, 10, 10, true)));*/
           // ((Button)view).invalidate();
        }
        else if(tag.toString() != adapt_obj.city_code_array[a]){

            /*((Button)view).setFocusable(true);
            selectedcityidclicked(tag.toString(), view);
            Drawable dr2 = getResources().getDrawable(R.drawable.location_btn_active_1);// inactive
            Bitmap bitmap2 = ((BitmapDrawable) dr2).getBitmap();
            // Scale it to 50 x 50
            Drawable d2 = new BitmapDrawable(Bitmap.createScaledBitmap(bitmap2, 10, 10, true));
            // Set your new, scaled drawable "d"
            ((Button)view).setBackgroundDrawable(d2);
            ((Button)view).invalidate();
            clicked = false;*/
        }
    }

    /*else if(tag.toString() != adapt_obj.city_code_array[a]){
         ((Button)view).setFocusable(true);
        Drawable dr2 = getResources().getDrawable(R.drawable.location_btn_inactive_1);//instead of inactive
        Bitmap bitmap2 = ((BitmapDrawable) dr2).getBitmap();
        // Scale it to 50 x 50
        Drawable d2 = new BitmapDrawable(Bitmap.createScaledBitmap(bitmap2, 10, 10, true));
        // Set your new, scaled drawable "d"
        ((Button)view).setBackgroundDrawable(d2);
        ((Button)view).invalidate();
    }
    else {
        Drawable dr2 = getResources().getDrawable(R.drawable.location_btn_active_1);//white
        Bitmap bitmap2 = ((BitmapDrawable) dr2).getBitmap();
        // Scale it to 50 x 50
        Drawable d2 = new BitmapDrawable(Bitmap.createScaledBitmap(bitmap2, 10, 10, true));
        // Set your new, scaled drawable "d"
        ((Button)view).setBackgroundDrawable(d2);
        ((Button)view).invalidate();
    }*/

    /*((Button) view).setText("*");
    //selectedcityidclicked(tag.toString());
    ((Button) view).setEnabled(false);*/
}
private void DeselectButtons() {
    // TODO Auto-generated method stub
    for(int x=0; x<adapt_obj.city_code_array.length;x++){ 
        System.out.println("value of enable tag::" +enablebutton);
        System.out.println("valuie dofdsf xxx:::" +x);
        if (enablebutton!= x)
            //What should i do here as i had to take the un clicked button as set selected as false //
            // ((Button)view).setSelected(false);
            //this.findViewById(i).setSelected(false);
            //this.findViewById(i).setSelected(false);
    }
}
private void selectedcityidclicked(String cityval, View view) {
     cityidvalue = "&city_code="+cityval;
     System.out.println("value of cityidvalue::" +cityidvalue);

     new MyAsyncTask(view).execute();
}

2 个答案:

答案 0 :(得分:1)

您可以将此选择器用作您的按钮的drawable,当您单击它或将其选中时,它将更改其drawable。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="true" android:drawable="@drawable/home_hover"></item>
    <item android:state_selected="true" android:drawable="@drawable/home_selected"></item>
    <item android:drawable="@drawable/home"></item>
</selector>

要取消选择其他按钮,您需要将它们循环播放,例如将所有按钮设置为线性或相对布局,而不是通过其子项获取该相对布局循环的引用并查看其标签。如果它们等于你想要取消选择的按钮标记,而不是将setSelected(false)调用到该按钮。

private void DeselectButtons() {
    LinearLayout layout = findViewById(R.id.parent_ofbuttons);
    for(int i=0; i<layout.getChildCount(); i++) {
         Button btn = (Button)layout.getChildAt(i);  
         if(btn.getTag().toString().equals("100")) {
              btn.setSelected(false);
         }
    }
}

答案 1 :(得分:0)

创建一个选择器xml,通常是一个xml

在项目中设置你的图像为android:state_pressedpressed =“true”和android:state_pressed =“false”

my_button_background.xml

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

然后将此文件保存在drawable中,然后将按钮背景设置为此文件

myButton.setBackgroundResourceId(R.drawable.my_button_background);

-----------------------------

编辑

button1.setOnClickListener(this);
button2.setOnClickListener(this);

现在在你的覆盖onCLick

@Override
OnClick(View v)
{
   if(v.getId() == R.id.button1_id)
   {
      button1.setBackgroundResource(R.drawable.anyImage1);
      button2.setBackgroundResource(R.drawable.anyImage2);
   }
   else
   {
 if(v.getId() == R.id.button1_id)
   {
      button1.setBackgroundResource(R.drawable.anyImage2);
      button2.setBackgroundResource(R.drawable.anyImage1);
   }
   }
}

别忘了声明button1和button2成员变量