RadioButton的selector以及切换界面

这里写图片描述
首先是RadioButton的背景selector

左边的selector

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

    <item android:state_checked="true">
        <shape>
            <corners
                android:topLeftRadius="5dp"
                android:bottomLeftRadius="5dp"/>

            <solid android:color="@color/green"/>
        </shape>
    </item>

    <item>
        <shape>
            <corners
                android:bottomLeftRadius="5dp"
                android:topLeftRadius="5dp" />

            <solid android:color="@color/white" />

            <stroke
                android:width="1dp"
                android:color="@color/green" />

        </shape>
    </item>

</selector>

中间的selector

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

    <item android:state_checked="true">
        <shape>

            <solid android:color="@color/green"/>
        </shape>
    </item>

    <item>
        <shape>

            <solid android:color="@color/white" />

            <stroke
                android:width="1dp"
                android:color="@color/green" />

        </shape>
    </item>

</selector>

右边的selector

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

    <item android:state_checked="true">
        <shape>
            <corners
                android:topRightRadius="5dp"
                android:bottomRightRadius="5dp"/>

            <solid android:color="@color/green"/>
        </shape>
    </item>

    <item>
        <shape>
            <corners
                android:bottomRightRadius="5dp"
                android:topRightRadius="5dp" />

            <solid android:color="@color/white" />

            <stroke
                android:width="1dp"
                android:color="@color/green" />

        </shape>
    </item>

</selector>

字体的selector

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/white" android:state_checked="true"/>
    <item android:color="@color/white" android:state_focused="true"/>
    <item android:color="@color/white" android:state_pressed="true"/>
    <item android:color="@color/green"/>
</selector>

activity_main.xml ,上面是三个RadioButton,下面是button对应的layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical">

    <RadioGroup
        android:id="@+id/rg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:checkedButton="@+id/rb_train"
        android:gravity="center"
        android:padding="10dp"
        android:orientation="horizontal" >

        <RadioButton
            android:id="@+id/rb_train"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:background="@drawable/sel_radio_left"
            android:button="@null"
            android:gravity="center"
            android:padding="10dp"
            android:text="培训"
            android:textColor="@drawable/sel_radio_text"
            android:textSize="12sp" />

        <RadioButton
            android:id="@+id/rb_evaluate"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:background="@drawable/sel_radio_mid"
            android:button="@null"
            android:gravity="center"
            android:padding="10dp"
            android:text="评测"
            android:textColor="@drawable/sel_radio_text"
            android:textSize="12sp" />

        <RadioButton
            android:id="@+id/rb_examRecord"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:background="@drawable/sel_radio_right"
            android:button="@null"
            android:gravity="center"
            android:padding="10dp"
            android:text="考试记录"
            android:textColor="@drawable/sel_radio_text"
            android:textSize="12sp" />
    </RadioGroup>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <LinearLayout
            android:id="@+id/ll_train"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="培训 " />

            <ListView
                android:id="@+id/lv_train"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/ll_evaluate"
            android:visibility="gone"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="评测 " />

            <ListView
                android:id="@+id/lv_evaluate"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/ll_examRecord"
            android:visibility="gone"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="考试记录 " />

            <ListView
                android:id="@+id/lv_examRecord"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />
        </LinearLayout>

    </LinearLayout>

</LinearLayout>

MainActivity

package com.lzz.demoapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioGroup;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {

    private RadioGroup rg;
    private LinearLayout ll_train,ll_evaluate,ll_examRecord;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        rg = (RadioGroup) findViewById(R.id.rg);
        rg.setOnCheckedChangeListener(this);
        ll_train = (LinearLayout) findViewById(R.id.ll_train);
        ll_evaluate = (LinearLayout) findViewById(R.id.ll_evaluate);
        ll_examRecord = (LinearLayout) findViewById(R.id.ll_examRecord);
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
        if (checkedId == R.id.rb_train) {
            clickTrain();
        } else if (checkedId == R.id.rb_evaluate) {
            clickEvaluate();
        } else if (checkedId == R.id.rb_examRecord) {
            clickExamRecord();
        }
    }

    private void clickExamRecord() {
        ll_train.setVisibility(View.GONE);
        ll_evaluate.setVisibility(View.GONE);
        ll_examRecord.setVisibility(View.VISIBLE);
    }

    private void clickEvaluate() {
        ll_train.setVisibility(View.GONE);
        ll_evaluate.setVisibility(View.VISIBLE);
        ll_examRecord.setVisibility(View.GONE);
    }

    private void clickTrain() {
        ll_train.setVisibility(View.VISIBLE);
        ll_evaluate.setVisibility(View.GONE);
        ll_examRecord.setVisibility(View.GONE);
    }
}

以上。

要使用 Python 的 tkinter 模块实现页面切换,可以使用 radiobutton(单选按钮)来控制切换。下面是一个简单的示例代码: ```python import tkinter as tk class PageOne(tk.Frame): def __init__(self, parent, controller): tk.Frame.__init__(self, parent) self.controller = controller label = tk.Label(self, text="Page One") label.pack() class PageTwo(tk.Frame): def __init__(self, parent, controller): tk.Frame.__init__(self, parent) self.controller = controller label = tk.Label(self, text="Page Two") label.pack() class PageThree(tk.Frame): def __init__(self, parent, controller): tk.Frame.__init__(self, parent) self.controller = controller label = tk.Label(self, text="Page Three") label.pack() class MyApp(tk.Tk): def __init__(self): tk.Tk.__init__(self) container = tk.Frame(self) container.pack(side="top", fill="both", expand=True) container.grid_rowconfigure(0, weight=1) container.grid_columnconfigure(0, weight=1) self.pages = {} for Page in (PageOne, PageTwo, PageThree): page = Page(container, self) self.pages[Page] = page page.grid(row=0, column=0, sticky="nsew") self.show_page(PageOne) def show_page(self, page_to_show): page = self.pages[page_to_show] page.tkraise() app = MyApp() app.mainloop() ``` 在这个示例中,我们定义了三个页面类:`PageOne`、`PageTwo` 和 `PageThree`。每个页面类都继承自 `tkinter.Frame`,并具有一个 `controller` 参数,用于在页面之间进行切换。 然后,我们创建了一个继承自 `tkinter.Tk` 的 `MyApp` 类,其中包含了一个用于容纳页面的 `container`。我们使用 `grid` 布局将每个页面放入容器中,并使用 `show_page` 方法来切换页面。 在 `MyApp` 的构造函数中,我们创建了三个页面实例,并将它们保存到 `self.pages` 字典中。我们默认显示第一个页面(即 `PageOne`),但你可以根据需要更改。 你可以运行这段代码来查看页面切换的效果。选择不同的 radiobutton切换到相应的页面。希望对你有所帮助!如果还有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值