ImageButton and EditText

来源:互联网 发布:撤销资阳市知乎 编辑:程序博客网 时间:2024/06/08 14:09

ImageButton与Button类似,区别在于ImageButton可以自定义一张图片作为一个按钮,也正是因为使用了图片作为按钮,所以ImageButton按下与抬起的效果需要自己定义

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".MainActivity" >    <ImageButton        android:id="@+id/imageButton"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:background="@drawable/ic_launcher"        /></RelativeLayout>
package com.example.textavd;import android.app.Activity;import android.os.Bundle;import android.view.Menu;import android.view.MotionEvent;import android.view.View;import android.view.View.OnTouchListener;import android.widget.ImageButton;public class MainActivity extends Activity {    ImageButton imagebutton;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        imagebutton=(ImageButton) findViewById(R.id.imageButton);        //为图片设置触屏监听器        imagebutton.setOnTouchListener(new OnTouchListener(){            @Override            public boolean onTouch(View v, MotionEvent event) {                // TODO Auto-generated method stub            //用户为按下 if(event.getAction()==MotionEvent.ACTION_DOWN){            设置图片背景图     imagebutton.setBackgroundDrawable(getResources().getDrawable(R.drawable.a));                }else //用户为抬起             if(event.getAction()==MotionEvent.ACTION_UP){                    imagebutton.setBackgroundDrawable(getResources().getDrawable(R.drawable.ic_launcher));                }                return false;            }        });    }}

//在上面的 onTouch(View v, MotionEvent event) 的参数说明如下
第一个参数:表示触发出品事件的事件源View
第二个参数:表示触发事件的类型,如按下,抬起,移动
imagebutton.setBackgroundDrawable(getResources().getDrawable(R.drawable.a)
传入图片的ID得到一个Drawable对象。

因为EditText比较简单,所以在这也说一下

 <EditText         1.android:numeric="integer"        2.android:maxLines="1"        3.android:singleLine="true"        4.android:password="true"        5.android:hint="66"        />

第一个代表只能输入数字
第二个代表输入的最内容
第三个代表输入的内容只能在一行
第四个代表当你输入时是我隐藏的。显示*
第5个代表当你没有输入内容时