单击默认按钮



你好,我有问题。我在一个视图中有三个按钮,每当加载视图时,所有三个按钮都会显示,但默认情况下,没有一个按钮显示为单击。我想做的是,当加载该视图时,我希望三个按钮中的第一个以点击的形式显示,其数据显示在下面的列表视图中。有人能帮忙吗。。。

这是我的布局,我有三个按钮和一个列表视图,每个按钮都有一个要在列表视图中显示的列表。这个页面是在启动屏幕之后加载的,我想要的是当页面每次加载时,在listview中显示与它相关联的列表的第一个按钮。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <LinearLayout
        android:id="@+id/calender_layout"
        android:layout_width="fill_parent"
        android:layout_height="@dimen/dimention_of_background_of_button"
        android:background="#06960F"
        android:orientation="horizontal"
        android:weightSum="3" >
        <Button
            android:id="@+id/nearby_btn"
            android:layout_width="0dp"
            android:layout_height="@dimen/dimention_of_button"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:background="@drawable/btn_nearby_selected"/>
        <Button
            android:id="@+id/myevent_btn"
            android:layout_width="0dp"
            android:layout_height="@dimen/dimention_of_button"
            android:layout_gravity="center"
            android:layout_weight="1" 
            android:background="@drawable/btn_myevents"/>
        <Button
            android:id="@+id/fav_btn"
            android:layout_width="0dp"
            android:layout_height="@dimen/dimention_of_button"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:background="@drawable/btn_favorite"/>
    </LinearLayout>
    <ListView
        android:id="@+id/Eventlist"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/calender_layout" 
        android:layout_above="@+id/cal_footer_layout">
    </ListView>
    <LinearLayout
        android:id="@+id/cal_footer_layout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="#06960F"
        android:orientation="horizontal"
        android:weightSum="100" >
        <Spinner
            android:id="@+id/Searchspinner"
            android:layout_width="0dp"
            android:layout_height="@dimen/spinner_selection_height"
            android:layout_gravity="center"
            android:layout_weight="40"
            android:background="#06960F"
            android:spinnerMode="dropdown" />
        <EditText
            android:id="@+id/EnterDataToSearch"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="60"
            android:drawableLeft="@drawable/search_icon"
            android:inputType="text" />
    </LinearLayout>
</RelativeLayout>

这里有一个简短的例子,说明了我对注释的理解。

<RadioGroup android:id="@+id/typeGroup"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:weightSum="3">
            <ToggleButton 
                android:id="@+id/hotBtn"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:checked="true"
                style="@style/HotButtonSmall"/> 
            <ToggleButton 
                android:id="@+id/coldBtn"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                style="@style/ColdButtonSmall"/>
            <ToggleButton 
                android:id="@+id/frozenBtn"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                style="@style/FrozenButtonSmall"/>
   </RadioGroup>    

我把它们放在RadioGroup中,所以一次只检查一个。我的styles只是为它们所处的状态设置了某些图标。第一个按钮上的android:checked="true"在加载布局时将其设置为选中。

删除文本(如果需要)

在我的styles.xml中,我有

<item name="android:textOff"></item>
<item name="android:textOn"></item>

对于每个相应的样式,以便在状态更改时不会显示"关闭"/"打开"。

单击事件

由于这些都在RadioGroup中,我使用

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)

当按下按钮而不是onClick() 时触发必要的代码

CompoundButton文档

编辑

当你第一次加载Activity时,只需使用一个普通按钮并"点击"它,你就可以简单地进行

firstBtn.performClick();  //assuming firstBtn is the name of the button you want clicked

在初始化按钮后,在onCreate()中。您必须设置一个状态选择器来更改按钮在按下/单击时的外观。

最新更新