当前位置:首页 > 编程笔记 > 正文
已解决

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

来自网友在路上 194894提问 提问时间:2023-11-19 05:32:54阅读次数: 94

最佳答案 问答题库948位专家为你答疑解惑

<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()}
}
查看全文

99%的人还看了

猜你感兴趣

版权申明

本文"Kotlin语言实现单击任意TextVIew切换一个新页面,并且实现颜色变换":http://eshow365.cn/6-38960-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!