Android 中屏幕点击事件的实现2

Android技术学习,更多知识请访问https://www.itkc8.com

本文通过罗老师的视频总结得来的

1,一个xml文件

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button android:id="@+id/commonButton" android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/myButton1"/>
    <Button android:id="@+id/imageButton" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:layout_gravity="center"
        android:background="@drawable/av" android:text="@string/button2"/>
</LinearLayout>


2通常用的是实现OnClickListener,OnTouchListener,OnFocusChangeListener,OnKeyListener

 

 

//实现几个常用的点击事件
public class MainActivity extends Activity 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 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) {
		// TODO Auto-generated method stub
		if(KeyEvent.ACTION_DOWN == event.getAction()){//按下事件
			v.setBackgroundResource(R.drawable.a2);
		}else if(KeyEvent.ACTION_UP == event.getAction()){
			v.setBackgroundResource(R.drawable.a3);
		}
		return false;
	}
	
	//焦点发生变化触发
	@Override
	public void onFocusChange(View v, boolean hasFocus) {
		// TODO Auto-generated method stub
		if(hasFocus){
			imageButton.setBackgroundResource(R.drawable.a2);
		}else{
			imageButton.setBackgroundResource(R.drawable.a3);
		}
	}
	
	//触摸时候触发
	@Override
	public boolean onTouch(View v, MotionEvent event) {
		// TODO Auto-generated method stub
		if(event.getAction() == MotionEvent.ACTION_UP){
			v.setBackgroundResource(R.drawable.a2);
		}else if(event.getAction() == MotionEvent.ACTION_DOWN){
			v.setBackgroundResource(R.drawable.a3);
		}
		return false;
	}

	//点击时候触发
	@SuppressWarnings("deprecation")
	@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() < 10){
			value = 1;
		}
		button.setWidth(button.getWidth()+(int)(button.getWidth()*0.1)*value);
		button.setHeight(button.getHeight()+(int)(button.getHeight()*0.1)*value);
	}


3.实现的效果

 

Android技术学习,更多知识请访问https://www.itkc8.com

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值