1

私は次のような行レイアウトのリストビューを持っています

<?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="match_parent"
    android:orientation="horizontal" 
    android:weightSum="5"
    >

        <TextView 
        android:layout_width="0dp"
        android:layout_height="wrap_content" 
        android:id="@+id/ArriveTime"
        android:gravity="center" 
        android:layout_weight="1" />


        <TextView 
        android:layout_width="0dp"
        android:layout_height="wrap_content" 
        android:id="@+id/Name"
        android:gravity="center" 
        android:layout_weight="1" />

        <Button
        android:focusable="false"

        android:layout_width="0dp"
        android:layout_height="wrap_content" 
        android:id="@+id/Arrive"
        android:gravity="center" 
        android:layout_weight="1"           
        android:text="Arrive" />

        <Button
        android:focusable="false"

        android:layout_width="0dp"
        android:layout_height="wrap_content" 
        android:id="@+id/Encounter"
        android:gravity="center" 
        android:layout_weight="1"           
        android:text="Encounter" />

        <Button
         android:focusable="false"

        android:layout_width="0dp"
        android:layout_height="wrap_content" 
        android:id="@+id/Exit"
        android:gravity="center" 
        android:layout_weight="1"           
        android:text="Exit" />




</LinearLayout>

行をクリックするとボタンをクリックできるようにしたいので、設定android:focusable="false"してクリックすると言います

    MainGrid.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                  int position, long id) {

             Log.d("zzzzzzzzzzzzzzzzzzzzzzzz",""+ oldPosition);

            if(oldPosition != -1)
            {
                MainGrid.getChildAt(oldPosition).setFocusable(false);
                //MainGrid.findViewById(oldPosition).setFocusable(false);
            } 

            // Set the whole list view unfocusable 
             MainGrid.setFocusable(false);


            // focus only on this 
            view.setFocusable(true);

            oldPosition = position -1;

            SetMenueOnClick(position-1) ;
 }});

問題は、行を最初にクリックした後、ボタンがアクティブになり、それを修正するためのアイデアを再度クリックできないことです。クリックした行のボタンをアクティブにしてから、フォーカスを行に転送してさらにクリックする必要があります

4

3 に答える 3

3

ラインとボタンの両方を (常に) クリック可能にしたい場合は、これらすべての回転を行う必要はありません。次の 2 行を行 xml のボタン コールアウトに追加するだけです。

    android:focusable="false"
    android:focusableInTouchMode="false"

次に、各ボタンのアダプターでボタンクリックリスナーを設定し、通常どおりリストを呼び出すアクティビティでアイテムクリックリスナーを設定します。

行をクリックするまでボタンを無効にしたい場合(それが目標なのか、両方をクリック可能にしようとしていた方法の単なる後遺症なのかはわかりませんでした)、xmlでの設定は次のとおりですフォーカス可能なコールアウトと同じように、ボタンを無効にonListItemClick設定し、フラグを使用してステートメントを設定し、と を使用しifてクリック可能とクリック不可に切り替えます。button.setEnabled(false);button.setEnabled(true);

于 2012-06-12T14:34:13.023 に答える
0

次を使用して試すことができます:

android:descendantFocusability="blocksDescendants"

各要素を個別に変更する代わりに、レイアウト XML で。

リストビューの問題のボタンの詳細については、こちらをご覧ください

特にコメント #27 & #31

それが役に立てば幸い

于 2012-06-12T14:33:32.963 に答える
0

setFocusable(true) 以外に、setChecked(boolean b) も使用できます。ただし、このメソッドは CompoundButton で使用できます。

Java から:

CompoundButton cb = (CompoundButton) findViewById(R.id.yourButton); 
cb.setChecked(true);

XML で

<CompoundButton
    android:id="@+id/yourButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/button_stats"
    android:text="@string/stat_hard">
</CompoundButton>

ここで、button_stats.xml は、次の行を含む res/drawable にあるファイルです。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/generic_button_normal" android:state_checked="false"/>
<item android:drawable="@drawable/generic_button_checked" android:state_checked="true"/>
</selector>
于 2012-06-12T14:31:28.000 に答える