zoukankan      html  css  js  c++  java
  • 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图片
     
     
     
     
  • 相关阅读:
    JUC-狂神笔记整理学习
    多线程-学习笔记
    Redis分布锁
    Redis
    springcloud一个简单的基本流程
    Nacos
    mysql单表查询
    mysql多表查询
    mysql数据库
    mysql详细安装教程以及1067错误代码解决方案
  • 原文地址:https://www.cnblogs.com/kingshow123/p/buttonfocus.html
Copyright © 2011-2022 走看看