Button按钮的焦点变化

 

    按钮在应用中起着至关重要的作用,下面介绍按钮的焦点的变化。

一、建立工程,如图

 

二、activity_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/commbutton"
        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:gravity="center"
        android:background="@drawable/button1"
        android:text="按钮" />

</LinearLayout>
View Code

三、MainActivity.java中代码

package com.study.android_button1;

import android.os.Bundle;
import android.app.Activity;
import android.view.KeyEvent;
import android.view.Menu;
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 Activity implements OnClickListener,OnTouchListener,OnFocusChangeListener,OnKeyListener{

    private int value = 1; //用于改变按钮的大小
    private Button commonButton,imageButton;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        commonButton = (Button)this.findViewById(R.id.commbutton);
        imageButton = (Button)this.findViewById(R.id.imagebutton);
        commonButton.setOnClickListener(this);
        imageButton.setOnClickListener(this);
        imageButton.setOnClickListener(this);
        imageButton.setOnTouchListener(this);
        imageButton.setOnFocusChangeListener(this);
        imageButton.setOnKeyListener(this);
        
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if(KeyEvent.ACTION_DOWN == event.getAction()){
            v.setBackgroundResource(R.drawable.button3);
        }else if(KeyEvent.ACTION_UP == event.getAction()){
            v.setBackgroundResource(R.drawable.button2);
        }
        
        return false;
    }


    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if(hasFocus){
            imageButton.setBackgroundResource(R.drawable.button2);
        }else {
            imageButton.setBackgroundResource(R.drawable.button1);

        }
        
    }


    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if(event.getAction() == MotionEvent.ACTION_UP){
            v.setBackgroundResource(R.drawable.button1);
        }else if(event.getAction() == MotionEvent.ACTION_DOWN){
            v.setBackgroundResource(R.drawable.button2);
        }
        
        return false;
    }


    @Override
    public void onClick(View v) {
        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);
        
    }
    
}
View Code

四、效果图

五、button图片
 
 
 
 

转载于:https://www.cnblogs.com/kingshow123/p/buttonfocus.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值