`

TextView点击效果(Button)

阅读更多
TextView点击效果

演示的是一个用TextView来定义的一个Button,实现类似TabWidget风格的选项卡。
自定义按钮,这里没有通过Button类或者子类去做派生,而是通过TextView派生出来的。
在这里三个按钮是三个TextView派生类实例,中间的白线,是1px宽的白色矩形,这样就可以做出类似上面的效果。

效果图:
[img]

[/img]

工程结构图:
[img]

[/img]

/res/drawable/background_color.xml  用shape标签自定义一个渐变背景
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <gradient 
        android:startColor="#FFFFFFFF"
        android:endColor="#FFFFFFFF"
        android:angle="270.0"
        android:centerY="0.3"
        android:centerColor="#FFBDBDBD"
        />
</shape>


res/drawable/button_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:constantSize="true">
	<!-- 获得焦点时的背景图片 -->
	<item
		android:state_focused="true">
		<shape>
			<gradient
				android:startColor="#FFE5CF33"
				android:endColor="#FFF1E7A2"
				android:angle="90.0" />
		</shape>
	</item>
	<!-- 设置相应所有事件 -->
	<item
		android:state_enabled="true"
		android:state_pressed="false">
		<shape>
			<gradient
				android:startColor="#FF1B1B1B"
				android:endColor="#FF969696"
				android:angle="90.0" />
		</shape>
	</item>
	<!-- 按钮点击时的背景 -->
	<item
		android:state_enabled="true"
		android:state_pressed="true">
		<shape>
			<gradient
				android:startColor="#FF000000"
				android:endColor="#FF474747"
				android:angle="90.0" />
		</shape>
	</item>
	<item
		android:state_enabled="false"
		android:state_pressed="true">
		<shape>
			<gradient
				android:startColor="#FF000000"
				android:endColor="#FF474747"
				android:angle="90.0" />
		</shape>
	</item>
	<!-- 默认情况下的背景 -->
	<item>
		<shape>
			<gradient
				android:startColor="#FF000000"
				android:endColor="#FF474747"
				android:angle="90.0" />
		</shape>
	</item>
</selector>



res/layout/main.xml,这个是主布局,由自定义的Button和1px的白色矩形组成
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"
	android:background="@drawable/background_color"
	android:orientation="vertical">

	<LinearLayout
		android:layout_width="fill_parent"
		android:layout_height="10dip" />
	<LinearLayout
		android:layout_width="fill_parent"
		android:layout_height="40dip">
		<com.amaker.testbutton.TextButton
			android:layout_width="fill_parent"
			android:layout_height="fill_parent"
			android:layout_weight="1"
			android:text="饮食"
			android:gravity="center"
			android:background="@drawable/button_selector"
			android:focusable="true"
			android:clickable="true" />
		<View
			android:layout_width="2px"
			android:layout_height="fill_parent"
			android:background="#FFFFFFFF" />
		<com.amaker.testbutton.TextButton
			android:layout_width="fill_parent"
			android:layout_height="fill_parent"
			android:layout_weight="1"
			android:text="旅行"
			android:gravity="center"
			android:background="@drawable/button_selector"
			android:focusable="true"
			android:clickable="true" />
		<View
			android:layout_width="2px"
			android:layout_height="fill_parent"
			android:background="#FFFFFFFF" />
		<com.amaker.testbutton.TextButton
			android:layout_width="fill_parent"
			android:layout_height="fill_parent"
			android:layout_weight="1"
			android:text="体育"
			android:gravity="center"
			android:background="@drawable/button_selector"
			android:focusable="true"
			android:clickable="true" />
	</LinearLayout>

</LinearLayout>


继承自TextView的自定义Button:
package com.amaker.testbutton;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView; 
import android.widget.Toast;

public class TextButton extends TextView {
    public TextButton(Context context)
    {
        super(context);
    }
    public TextButton(Context context, AttributeSet attrs, int defStyle)
    {
        super(context,attrs,defStyle);
    }
    public TextButton(final Context context, AttributeSet attrs)
    {
        this(context,attrs,0);
        this.setOnTouchListener(new OnTouchListener()
        {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction()==MotionEvent.ACTION_CANCEL
                        ||event.getAction()==MotionEvent.ACTION_UP
                        ||event.getAction()==MotionEvent.ACTION_OUTSIDE)
                {
                    Toast.makeText(context, "hello", Toast.LENGTH_SHORT).show();
                }
                return false;
            }
            
        });
    }
}


主程序:
package com.amaker.testbutton;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}
  • 大小: 15.6 KB
  • 大小: 30.7 KB
分享到:
评论

相关推荐

    Android简单实现加减乘除(两个EditText,两个TextView,四个button)

    #Android简单实现加减乘除(两个EditText,两个TextView,四个button) ##界面效果 MainActivity.java package com.example.test1_1; import android.app.Activity; import android.os.Bundle; import ...

    DD登录 Android考试题目.zip

    多个控件,TextView,EditText,Button, Button,提交数据。 EditText填写数据。 TextView显示标题 功能说明 两个Activity内完成,相应的控制和显示。 主界面中,主布局中嵌套控制登录控件,嵌套布局...

    Android触控图片各种效果

    Android触控图片各种效果,点击触动图片,边缘动画效果,包括TextView,button,RelativeLayout,LinearLayout

    Android 单击选中的ImageButton[]图像数组用法示例.rar

    图像数组用法示例,类似于Radio的功能,不过用图片表现,这样可使Android UI更加友好,视觉更漂亮一些,用户轻触图片,即可选中该数据项,比Radio用户体验更好,如截图所示的选中效果,选中后图像会变色: ...

    Android编程基础之简单Button事件响应综合提示控件Toast应用示例

    本文实例讲述了Android简单Button事件响应综合提示控件Toast应用。...以下的Demo,我们将实现当点击Button时,TextView文字将发生改变,并在屏幕上出现一段时间的Toast提醒. 让我们看一下效果图: 点击按钮

    简单的Fragment动态添加.zip

    上方的顶部标签栏可进一步分为2部分,上方文字部分需要能够点击发生变化,并且每次只能选中一个,因此可以用RadioButton、Button、TextView来实现;下方横线部分可以使用TextView实现。在这里我们采用RadioButton来...

    android 录音笔demo!!!(Baseadapter、file、MediaRecorder、AlertDialog)

    listview的item中需要为button设置不同的点击效果等等。 为了便于新手学习,在此还是罗列一下涉及的主要知识点: 1、Baseadapter 2、JAVA的file 3、MediaRecorder 4、较多的AlertDialog 5、MediaPlayer

    Android中获取手机屏幕大小的方法

    运行程序后,当我们点击Button按钮时,可以看到下面的效果图: 具体代码: 我们可以通过使用类DisplayMetrics来获取手机屏幕的分辨率大小。DisplayMetrics类是获取手机屏幕各种属性的关键类。下面通过例子来展示...

    HelloWorld.zip

    Android开发之Button圆角形状+按压效果实现+点击事件处理(最基本的内容); 含TextView跑马灯内容; 完整项目包;

    星座说明书

    本程序段主要进行了查询主界面的设计,界面布局主要括:四个TextView、一个Button及其一个Spinner组件。其中Spinner是用来显示列表项的控件,类似于一组单选框RadioButton也就是下拉菜单。同时,数据的传送是通过...

    Android实现去哪儿携程地址互换效果

    昨天朋友项目中有个需求让我帮忙看看怎么搞,就跟去哪儿携程买机票时点中间按钮互换出发地和目的地的效果,当时一看觉得挺简单,用补间动画,在动画完成时设置给两边各textview互换值就好,做出来后发现效果不好,在...

    Android 自定义AlertDialog对话框样式

    点击首页的Button即跳出对话框,显示WIFI信息(TextView),密码输入框(EditText),取消和连接按钮(Button) 实现 根据自己实际的需求,为AlertDialog创建一个布局,在此我需要定义一个如图所示的WIFI密码输入框,故在 res/...

    Android编程基础之获取手机屏幕大小(DisplayMetrics应用)示例

    这里主要用了三个对象TextView ,Button ,以及DisplayMetrics ,其中Displaymetrics 是取得手机屏幕大小的关键类,这个例子非常的简单,当我们点击按钮,触发事件,在TextView 里显示手机屏幕的宽高分辨率. ...

    Android单元测试-对Activity的测试

    #前提概要 ... 这篇文章主要介绍常见的Activity中的测试。 ...1、检测一个布局中的button和TextView是否正确。 2、从网络动态获取String到Activity界面显示,并且这个图片的URL是由Intent传递过来的。

    Android自定义View之组合控件实现类似电商app顶部栏

    分析:左右两边可以是TextView和Button,设置drawableTop即可,中间的看着像是EditText,但是用过淘宝天猫等类似app的话会发现点击搜索不是在当前Activit进行搜索的,是跳转到另外的页面进行的,所以用TextView然后设置...

    Android实现多媒体录音笔

    listview的item中需要为button设置不同的点击效果等等。 为了便于新手学习,在此还是罗列一下涉及的主要知识点: 1、Baseadapter 2、JAVA的file 3、MediaRecorder 4、较多的AlertDialog 5、MediaPlayer 遇到的...

    mooc_android_lesson7_计算器

    根据本讲学习内容,学会运用TableLayout、Button、TextView进行简单计算器的界面布局,以及掌握按钮的点击事件监听。 (1)创建一个计算器,可以完成带小数的加、减、乘、除四则运算 (2) 提交的作业包中含有应用...

    Android ImageTextButton 自定义控件事件

    自定义控件,主容器LinearLayout,内部包含ImageView和TextView,知识点包括动态实现图片的点击效果,自定义控件事件,容器视图修订.注释完整,多余代码较少,非常适合参考

    android实现模拟加载中的效果

    效果图: 原始界面 点击按钮运行  运行后界面 实现代码: public class MainActivity extends AppCompatActivity { private Button bt; private TextView tv; public ProgressDialog myDialog; @...

    高仿微信界面

    开始第一步前先预览一下效果。 第一步,界面。 界面的思路是利用ViewPager+Fragment实现,所以activity_main.xml中添加一个ViewPager。顶部和底部include的顶部栏和底部栏后面再说。 MainActivity的界面activity_...

Global site tag (gtag.js) - Google Analytics