2012-01-27 5 views
0

に一つだけのボタンを強調するためにどのように、私は動的に一つのボタンを押すことで、ここでbuttons.Now作成しています、私は別のボタンを押すと、このボタンが強調表示されることbutton.supposeを強調しています、最初に押されたボタンはハイライトされません。データベースの値に基づいて、私のプロジェクトではアンドロイド

私の質問は:他のボタンが押されるまで、常に押されたボタンが強調表示されます。私は第一、第二ボタンを押すと第二ボタンが強調表示された場合、それはhighlighted.laterだ1ボタンを押して、自分のコードで

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);  
     } 
    } 

+0

どのようにボタンを強調表示しますか? – papaiatis

答えて

0

私はあなたがグループではない通常のボタンビューでチェックボックスを使用する必要があると思います。これがはっきりしない場合は、私はあなたのためのいくつかのコードを作ることができます..しかし、基本的にチェックボックスを使用して表示することに同意するスタイルを変更します..この場合は、(チェック)を強調表示し、あなたがグループにコードを追加する

=======をすべてそれらを使用する場合、ボタン(チェックボックス)最初のチェックは=======

を消えてしまいます、ここでは私の友人を行く:

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:

mainactivityに

(オフスタイル描画可能など)

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

hlornd.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(ASセレクタスタイル):

<?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(チェックとしてスタイル描画可能)

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

hlornd/hlordスタイルの色を変更するだけです。

幸運...

+0

申し訳ありません私はあなたがexplain.i can notを使用してチェックボックス – sowjanyarendla

+0

すべてのボタンiamのデータベースvalues.soを押して、そのボタンを押すと、その値は画面に表示されます使用できません。そのボタンだけがハイライト表示されます – sowjanyarendla

+0

あなたには正しいコードがあります。 – sowjanyarendla

0

はこれを試してみてください:

2つのボタン、ボタン1とボタンを取るとします。

および2つのボタンのための4つの描画可能作成:

例:通常の画像: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) 
} 
+0

ここではボタンを動的に作成しています。どのようにして個々のonclicklistenerメソッドを記述できますか? for(int i = 0; i sowjanyarendla

+0

ボタンがすでにXMLファイルに作成されている場合、urコードはaplly.Plzに一度チェックして解決策を教えてください – sowjanyarendla

+0

すべてのボタンのidが異なりますか?そのIDでハンドルしてください – Abhi

1

ボタン要素は、状態を押しました。チェックボックスには、状態チェックと呼ばれる状態があります。ボタン画像付きのチェックボックスを作成するとよいでしょう。

関連する問題