当前位置: 首页 > news >正文

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"><!-- 这里放置你的其他视图组件 --><RadioGroupandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><RadioButtonandroid: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" /><RadioButtonandroid: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" /><!-- 更多的 TextView 组件 -->
</RadioGroup><FrameLayoutandroid:id="@+id/container"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_below="@id/textView2" /></LinearLayout>

创建一个新包 color

<?xml version="1.0" encoding="utf-8"?>
<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>

功能页

class MainActivity2 : AppCompatActivity() {private lateinit var fragmentManager: FragmentManagerprivate val binding by lazy {ActivityMain2Binding.inflate(layoutInflater)}override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(binding.root)fragmentManager = supportFragmentManagerbinding.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()}
}
http://www.lryc.cn/news/235001.html

相关文章:

  • Flume学习笔记(4)—— Flume数据流监控
  • 使用webhook发送企业微信消息
  • C语言的由来与发展历程
  • python django 小程序博客源码
  • Android并发编程与多线程
  • ChatGPT简介及基本概念
  • 学习模拟简明教程【Learning to simulate】
  • 电子学会C/C++编程等级考试2021年12月(一级)真题解析
  • 数字游戏
  • k8s pod 处于Terminating的原因分析和解决处理——筑梦之路
  • 西南科技大学814考研二
  • oracle21c报错 【ORA-65096: 公用用户名或角色名无效】
  • C++ 递增/递减运算符重载
  • Android 13.0 无源码app增加授予相关权限
  • CI/CD相关概念学习
  • 一、认识STM32
  • vue-router的编程式导航有哪些方法?
  • 连接服务器上mysql数据库
  • IDEA 中设置 File Header 以及自定义类、方法注释模板的方法
  • 【数据结构】图的存储结构及实现(邻接表和十字链表)
  • ROS Turtlebot3多机器人编队导航仿真
  • 端口配置错误,导致RabbitMq启动报错
  • <MySQL> 什么是JDBC?如何使用JDBC进行编程?
  • 基于安卓android微信小程序的装修家装小程序
  • 基于SSM的小区物业管理系统设计与实现
  • c语言免杀火绒
  • MyBatis #{} 和 ${} 的区别
  • 计算机科学速成课
  • 基于单片机的汽车安全气囊系统故障仿真设计
  • JPA整合Sqlite解决Dialect报错问题, 最新版Hibernate6