Android控件复习:按钮(Button)

主要内容:

1.按钮监听和长按按钮监听

2.“.9图片文件的使用”

3.自定义点击按钮效果

-图片模式

-颜色模式

实代码例:

1.部分代码

先来看布局文件main.xml

<?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:orientation="vertical" >
    
	<Button 
	    android:id="@+id/bt1"
	    android:layout_width="match_parent"
	    android:layout_height="wrap_content"
	    android:text="Button"
	    android:textSize="25px"/>
	<TextView 
	    android:id="@+id/tv1"
	    android:layout_width="match_parent"
	    android:layout_height="wrap_content"
	    android:textSize="30px"/>
	<Button 
	   android:layout_width="90dip"
   	   android:layout_height="45dip"
   	   android:layout_marginTop="1dip"
	   android:textSize="30px"
	   android:background="@drawable/image_btn_bird"
	   android:layout_gravity="center"/>
	<Button 
	    android:layout_width="match_parent"
	    android:layout_height="wrap_content"
	    android:text="button"
	   	android:background="@drawable/button_font"/>
	<Button 
	    android:layout_width="match_parent"
	    android:layout_height="wrap_content"
	    android:background="@drawable/send_button_off"/>
	<Button 
	    android:layout_width="match_parent"
	    android:layout_height="wrap_content"
	    android:background="@drawable/new1"/>
	<Button 
	    android:id="@+id/btTest"
	    android:layout_width="match_parent"
	    android:layout_height="wrap_content"
	    android:text="test"
	    android:onClick="selfDestruct"/>
	
	
</LinearLayout>

监听code

 //普通监听
        button1.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Toast.makeText(MainActivity.this, "点击了button", Toast.LENGTH_LONG).show();
			}
		});
        
        //长监听
        button1.setOnLongClickListener(new OnLongClickListener() {
			
			@Override
			public boolean onLongClick(View v) {
				// TODO Auto-generated method stub
				Toast.makeText(MainActivity.this, "长按button", Toast.LENGTH_LONG).show();
				return true;
			}
		});

2.      .9格式图片在开发中应用的非常多,我们不能保证每部手机都能显示标准,.9文件的图片可以根据屏幕来自动的伸缩,保证界面友好

可以看出在这个按钮中,我直接使用了new1.9.png的图片格式,黑线部分就是自己处理的,处理方法


<Button 
	    android:layout_width="match_parent"
	    android:layout_height="wrap_content"
	    android:background="@drawable/new1"/>
但是这个点击是没有效果的,只是孤零零的一张图片。我们下面加上判断来切换不同的图片显示按钮效果;

3.自定义效果

图片模式:code 使用true和false来判断

<?xml version="1.0" encoding="utf-8"?>   
<selector xmlns:android="http://schemas.android.com/apk/res/android">   
     <item android:state_pressed="true" android:drawable="@drawable/send_button_on" />  
     <item android:state_pressed="false" android:drawable="@drawable/send_button_off" />   
</selector> 
颜色模式:code 和图片时一类的
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
     <item android:state_pressed="false">
    	    <shape>
    	        <solid android:color="#ff9d77"/> 
			    <stroke android:width="2dp" android:color="#dcdcdc" />
			    <padding  android:left="30dp"  android:top="10dp"  
        					android:right="30dp"   android:bottom="10dp" />    
    	    </shape>
    </item>
    
     <item android:state_pressed="true">
    	    <shape>
    	         <solid android:color="#ff00ff"/> 
    	          <stroke android:width="2dp" android:color="#ff00ff" />
    	          <corners android:radius="2dp" /> 
    	    </shape>
    </item>
     
</selector>


我们可以在应用中反复调用我们的文件

源代码


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值