2012-05-15 122 views
0

我正在开发一个由按钮组成的应用程序。 这是我的问题,如何设置选中状态和未选中状态。按钮选中状态Android?

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

    <item android:drawable="@drawable/ox_mnu_dashboard_over" android:state_selected="true"/> 
    <item android:drawable="@drawable/ox_mnu_dashboard_normal"/> 

</selector> 

我正在使用此代码,但不适用于我。 下面是XML文件:

<FrameLayout 
     android:id="@+id/tabDashboard" 
     android:layout_width="60dip" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="2dip" 
     android:layout_weight="1" 
     android:clickable="true" > 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="bottom|center_horizontal" 
      android:focusableInTouchMode="true" 
      android:marqueeRepeatLimit="marquee_forever" 
      android:scrollHorizontally="true" 
      android:singleLine="true" 
      android:text="@string/dashboard" 
      android:textColor="@color/white" 
      android:textSize="12sp" /> 

     <ImageButton 
      android:id="@+id/btnDashboard" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:background="@drawable/tab_dashboard" 
      android:clickable="true" /> 
    </FrameLayout> 

回答

2

您可以设置图像时,点击上的ImageButton像下面的代码。

int isClicked = 0; 
image = (ImageButton) findViewById(R.id.image); 
    image.setOnClickListener(new OnClickListener() { 

     public void onClick(View v) { 
      if (isClicked1 == 0) { 
       image.setImageResource(R.drawable.second_image); 
       isClicked1 = 1; 
      } else { 
       image.setImageResource(R.drawable.original_image); 
       isClicked1 = 0; 
      } 

     } 
    });' 
+0

我需要选择的状态保持 –

+0

然后你需要设置图像,当你点击该特定的imageButton。 –

+0

@NikhilreddyGujjula我更新了我的答案。 –