0

検索オプションの長いリストがあります。リストの上下に「検索」するボタンが欲しい。両方のボタンの機能は同じです。最後にxmlレイアウトの最初のボタンをコピーして貼り付けるだけでよいと思いましたが、最後のボタンには機能がありません。アクティビティに余分なコードを追加せずにこれを機能させるにはどうすればよいですか?

<ScrollView 
    android:layout_width= "fill_parent"
    android:layout_height= "fill_parent" >

<LinearLayout
    android:id="@+id/LinearLayout02"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

<Button android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:id="@+id/save_search_options"
    android:text="Search" android:textSize="25dip"></Button>

<!--search options in between-->


<Button android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:id="@+id/save_search_options"
    android:text="Search" android:textSize="25dip"></Button>

</LinearLayout>
</ScrollView>

4

4 に答える 4

2

追加するだけです

Button
 ...
 android:onClick="functionName"

両方のボタンに、Activity好きな機能を作成します

public void functionName(View v)
{
     //some logic
}

次に、どちらを押したかがわかります。Buttonただし、1つを上に置いておく方が効率的かもしれませんList

于 2013-03-19T16:09:28.280 に答える
1

それぞれに同じものを付けるとonClickListener、同じ機能になります。android:onClickXML 属性を使用してコードを回避するために、XML でこれを行うことができます。

于 2013-03-19T16:08:41.647 に答える
0

コードが次のようになるように、2ボタンに別のIDを設定します

<ScrollView 
    android:layout_width= "fill_parent"
    android:layout_height= "fill_parent" >

<LinearLayout
    android:id="@+id/LinearLayout02"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

<Button android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:id="@+id/top_save_search_options"
    android:text="Search" android:textSize="25dip"></Button>

<!--search options in between-->


<Button android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:id="@+id/bottom_save_search_options"
    android:text="Search" android:textSize="25dip"></Button>

</LinearLayout>
</ScrollView>

設定したIDを変えてアクティビティのボタンを宣言すると、両方のボタンに同じリスナーを設定できます

于 2013-03-19T16:10:21.047 に答える
0

同じ ID を持つ 2 つのボタンがあります。それらの 1 つの名前を変更し、リスナーを両方に割り当てます。

于 2013-03-19T16:09:05.860 に答える