用TextView实现button点击效果

TextView是Android开发中最常用的控件,它的Api也十分丰富。这里仅仅介绍下使用TextView来实现的Button,点击效果的实现。
UI页面中任何一个可点击的控件,都应该有点击效果。
当TextView所在的控件被点击时,可以从以下三处实现点击效果:
1.如果TextView的drawableLeft、….drawableBottom属性赋有值,可以将icon的图标通过布局文件标签的方式实现;
如:res/drawable/xxx_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/xxx_pressed" />
    <item android:drawable="@drawable/xxx_normal" android:state_pressed="true"/>
</selector>

2.给TextView的父节点设置background,其background通过布局文件实现,如:res/drawable/common_cancel_btn_selector.xml

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

    <item android:state_pressed="true">
        <shape>
            <solid android:color="#f0f0f0" />
        </shape>
    </item>
    <item>
        <shape>
            <solid android:color="#ffffff" />
        </shape>
    </item>

</selector>

3.设置TextView的textColor,textColor通过布局文件标签实现,如:res/color/common_black_text_color_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="#323232" android:state_pressed="true" />
    <item android:color="#323232" android:state_selected="true" />
    <item android:color="#808080" />
</selector>

例如如下效果:
这里写图片描述
这里写图片描述
这里写图片描述
使用布局文件的是

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#f9f9f9"
    android:gravity="center"
    android:orientation="horizontal">

    <LinearLayout
        android:id="@+id/ll_cancel"
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:layout_weight="1"
        android:background="@drawable/common_cancel_btn_selector"
        android:gravity="center"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tv_cancel"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="取消"
            android:textColor="@color/common_black_text_color_selector"
            android:textSize="16sp" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/ll_sure"
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:layout_weight="1"
        android:background="@drawable/common_sure_btn_selector"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/tv_sure"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="确定"
            android:textColor="@color/common_white_text_color_selector"
            android:textSize="16sp" />
    </LinearLayout>
</LinearLayout>

同时需要在代码中设置ll_sure、ll_cancel的点击侦听:

        ll_cancel.setOnClickListener(this);
        ll_sure.setOnClickListener(this);

如果需要在代码中动态修改,实现方式如下:

tv_sure.setCompoundDrawablesWithIntrinsicBounds(id, 0, 0, 0);
ll_sure.setBackgroundResource(R.drawable.common_sure_btn_selector);
tv_sure.setTextColor(getResources().getColorStateList(R.color.common_white_text_color_selector));
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android Fragment 是 Android 开发中的一种重要的 UI 组件,它可以将 Activity 分割为多个独立的模块化部分,每个部分都有自己的生命周期和UI布局。 在 Android Fragment 中实现多个页面切换效果,可以通过 FragmentTransaction 和 FragmentManager 来实现。以下是一个简单的示例: 首先,需要在布局文件中定义一个用于显示 Fragment 的容器,例如: ``` <FrameLayout android:id="@+id/fragment_container" android:layout_width="match_parent" android:layout_height="match_parent" /> ``` 然后,在 Activity 中通过 FragmentTransaction 和 FragmentManager 来切换不同的 Fragment 页面。例如,点击按钮时切换到第一个 Fragment 页面: ``` Button button1 = findViewById(R.id.button1); button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.replace(R.id.fragment_container, new Fragment1()); fragmentTransaction.commit(); } }); ``` 同样地,可以实现其他的页面切换效果。例如,点击按钮时切换到第二个 Fragment 页面: ``` Button button2 = findViewById(R.id.button2); button2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.replace(R.id.fragment_container, new Fragment2()); fragmentTransaction.commit(); } }); ``` 在每个 Fragment 中,可以定制自己的界面和逻辑。例如,Fragment1 的界面布局可以在 fragment1.xml 文件中定义: ``` <LinearLayout ... android:orientation="vertical"> <TextView ... android:text="Fragment 1" /> ... </LinearLayout> ``` 通过实现多个不同的 Fragment 并切换它们,就可以实现多个页面的切换效果。这种方式可以让用户在同一个 Activity 中流畅地切换不同的页面,提升用户体验。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值