Android控件复习:按钮(Button)

来源:互联网 发布:中国气象数据网账号 编辑:程序博客网 时间:2024/05/17 23:41

主要内容:

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() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubToast.makeText(MainActivity.this, "点击了button", Toast.LENGTH_LONG).show();}});                //长监听        button1.setOnLongClickListener(new OnLongClickListener() {@Overridepublic boolean onLongClick(View v) {// TODO Auto-generated method stubToast.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>


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

源代码


原创粉丝点击