学习Button按钮控件(一)

Button控件的基本用法和TextView、EditText相似,最常用的是按钮单击事件。

实现功能:按钮的焦点变化

效果展示:(动图还蛮有趣的)



java代码:

package com.exale.hello;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnKeyListener;
import android.view.View.OnTouchListener;
import android.widget.Button;

public class MainActivity extends ActionBarActivity implements 
OnClickListener,OnTouchListener,OnFocusChangeListener,OnKeyListener{
private int value=1;//用于改变按钮的大小
private Button commonButton;
private Button imageButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        commonButton = (Button)this.findViewById(R.id.commonbutton);
        imageButton = (Button)this.findViewById(R.id.imagebutton);
        commonButton.setOnClickListener(this);
        imageButton.setOnClickListener(this);
        imageButton.setOnTouchListener(this);
        imageButton.setOnFocusChangeListener(this);
        imageButton.setOnKeyListener(this);       
    }
    
    @Override
public void onClick(View v) {
// TODO Auto-generated method stub
    Button button = (Button)v;
    if(value==1&&button.getWidth() == getWindowManager().getDefaultDisplay().getWidth())
    {
    value = -1;
    }else if(value==-1&&button.getWidth()<100)
    {
    value = 1;
    }
    button.setWidth(button.getWidth()+(int)(button.getWidth()*0.1*value));
    button.setHeight(button.getHeight()+(int)(button.getHeight()*0.1*value));

}
    
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if(KeyEvent.ACTION_DOWN==event.getAction())
{
v.setBackgroundResource(R.drawable.medimum);
}else if(KeyEvent.ACTION_UP == event.getAction())
{
v.setBackgroundResource(R.drawable.right);
}

return false;
}
    
@Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
if(hasFocus)
{
imageButton.setBackgroundResource(R.drawable.right);
}else
{
imageButton.setBackgroundResource(R.drawable.left);
}

}


@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if(event.getAction()==MotionEvent.ACTION_UP)
{
v.setBackgroundResource(R.drawable.left);
}else if(event.getAction()==MotionEvent.ACTION_DOWN)
{
v.setBackgroundResource(R.drawable.right);
}
return false;
}
}


XML中的Button设置:

<Button
    android:id="@+id/commonbutton"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="我的按钮一" />


<Button
    android:id="@+id/imagebutton"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center"
    android:background="@drawable/left"
    android:text="按钮" />

虽然很简单,但还是学习到了一些方法的调用与实现和Button控件的使用。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值