• Kotlin语言实现单击任意TextVIew切换一个新页面,并且实现颜色变换


    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_height="match_parent">
    
        
        <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        <RadioButton
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:button="@null"
            android:textColor="@color/color"
            android:checked="true"
            android:layout_marginHorizontal="20dp"
            android:layout_height="wrap_content"
            android:text="First TextView"
            android:clickable="true" />
    
        <RadioButton
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_marginHorizontal="20dp"
            android:button="@null"
            android:layout_height="wrap_content"
            android:text="Second TextView"
            android:textColor="@color/color"
            android:clickable="true"
            android:layout_below="@id/textView1" />
    
        
    RadioGroup>
    
    
        <FrameLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/textView2" />
    
    LinearLayout>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44

    创建一个新包 color

    
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/purple_700" android:state_checked="true"/>
        <item android:color="@color/black" android:state_checked="false"/>
    selector>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    功能页

    class MainActivity2 : AppCompatActivity() {
        private lateinit var fragmentManager: FragmentManager
        private val binding by lazy {
            ActivityMain2Binding.inflate(layoutInflater)
        }
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(binding.root)
            fragmentManager = supportFragmentManager
    
            binding.textView1.setOnClickListener {
                replaceFragment(HomeFragment())
            }
    
            binding.textView2.setOnClickListener {
                replaceFragment(SecondFragment())
            }
        }
        private fun replaceFragment(fragment: Fragment) {
            val transaction: FragmentTransaction = fragmentManager.beginTransaction()
            transaction.replace(R.id.container, fragment)
            transaction.addToBackStack(null)
            transaction.commit()
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
  • 相关阅读:
    Oracle 误删表后数据恢复操作
    论文项目总结01-常用样式总结
    如何改变胆小怕事的性格?
    前端基础入门之JS的call、apply和argument
    基于SSM的进销存管理系统设计与实现
    135. 分发糖果
    Clickhouse 的无 root 部署方案浅探
    武汉高性能计算大会2022举办,高性能计算生态发展再添新动力
    Selenium自动化测试(基于Java)
    线上购药小程序的崭新时代:医疗与科技的完美结合
  • 原文地址:https://blog.csdn.net/m0_62491934/article/details/134486527