按钮控件

监听器:



监听器 方法 内容
OnClickListener onClick 监听点击事件(点击或按下导航键)
OnClickListener onLongClick 监听长按事件(保持点击或按住导航键)
OnClickListener onKey 监听物理按件(点击或松开物理导航键,上下左右键)
OnTouchListener onTouch 监听触摸事件(点击滑动弹起等)



设置监听器:

方法一:定义一个OnClickListener类的实例,并使用setOnClickListener等绑定监听器。

方法二:用Activity来实现OnClickListener接口。

其中第一种通常用匿名内部类或者内部类来实现。而第二种更加简洁,更加高效,特别是在很多空间需要添加监听器的情况下。



界面:

Xml代码
<Button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="MyButton"
/>
<TextView
android:id="@+id/textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>

<Button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="MyButton"
/>
<TextView
android:id="@+id/textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/> 在onLongClick中,返回值必须为true;消费概念:当时间完成对一个view的操作后,只需要返回true,表示该控件已经被消费了,后面的时间就不会被调用了。

ACTION_DOWN 按下

ACTION_MOVE 滑动

ACTION_UP 弹起

ACTION_DOWN为起始事件,如果调用了其他两个方法,这个是肯定先被调用。

Java代码
package example.first;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
private Button bt;
private TextView tv;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

tv=(TextView)findViewById(R.id.textview);
bt=(Button)findViewById(R.id.button);

//匿名内部类作为监听器
bt.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
tv.setText("你点击了button");
}
});
bt.setOnLongClickListener(new OnLongClickListener(){

@Override
public boolean onLongClick(View arg0) {
// TODO Auto-generated method stub
tv.setText("你长按了button");
return true; //注意这里必须return true

}
});
}
@Override
public boolean onKeyDown(int keyCode,KeyEvent event){
switch(keyCode){
case KeyEvent.KEYCODE_DPAD_UP:tv.setText("你按下了上方向键");//监听按下上方向键
break;
case KeyEvent.KEYCODE_DPAD_DOWN:tv.setText("你按下了下方向键");
}
return super.onKeyDown(keyCode, event);

}
@Override
public boolean onKeyUp(int keyCode,KeyEvent event){
switch(keyCode){
case KeyEvent.KEYCODE_DPAD_UP:tv.setText("你松开了了上方向键");
break;
case KeyEvent.KEYCODE_DPAD_DOWN:tv.setText("你松开了下方向键");
}
return super.onKeyUp(keyCode, event);

}
public boolean onTouchEvent(MotionEvent event){
int x=(int)event.getX();
int y=(int)event.getY();
switch(event.getAction()){
case MotionEvent.ACTION_MOVE:tv.setText("你滑动了屏幕");
break;
case MotionEvent.ACTION_DOWN:tv.setText("你点击的屏幕坐标为"
+Integer.toString(x)+","+Integer.toString(y));//取得点击的坐标
break;
case MotionEvent.ACTION_UP:tv.setText("你离开屏幕的坐标为"
+Integer.toString(x)+","+Integer.toString(y));//取得松开时的坐标
break;
}
return true; //最好是return true
}
}

package example.first;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
private Button bt;
private TextView tv;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

tv=(TextView)findViewById(R.id.textview);
bt=(Button)findViewById(R.id.button);

//匿名内部类作为监听器
bt.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
tv.setText("你点击了button");
}
});
bt.setOnLongClickListener(new OnLongClickListener(){

@Override
public boolean onLongClick(View arg0) {
// TODO Auto-generated method stub
tv.setText("你长按了button");
return true; //注意这里必须return true

}
});
}
@Override
public boolean onKeyDown(int keyCode,KeyEvent event){
switch(keyCode){
case KeyEvent.KEYCODE_DPAD_UP:tv.setText("你按下了上方向键");//监听按下上方向键
break;
case KeyEvent.KEYCODE_DPAD_DOWN:tv.setText("你按下了下方向键");
}
return super.onKeyDown(keyCode, event);

}
@Override
public boolean onKeyUp(int keyCode,KeyEvent event){
switch(keyCode){
case KeyEvent.KEYCODE_DPAD_UP:tv.setText("你松开了了上方向键");
break;
case KeyEvent.KEYCODE_DPAD_DOWN:tv.setText("你松开了下方向键");
}
return super.onKeyUp(keyCode, event);

}
public boolean onTouchEvent(MotionEvent event){
int x=(int)event.getX();
int y=(int)event.getY();
switch(event.getAction()){
case MotionEvent.ACTION_MOVE:tv.setText("你滑动了屏幕");
break;
case MotionEvent.ACTION_DOWN:tv.setText("你点击的屏幕坐标为"
+Integer.toString(x)+","+Integer.toString(y));//取得点击的坐标
break;
case MotionEvent.ACTION_UP:tv.setText("你离开屏幕的坐标为"
+Integer.toString(x)+","+Integer.toString(y));//取得松开时的坐标
break;
}
return true; //最好是return true
}
}
运行效果:







Button效果;状态列表(statelist实现)

根标签setector

一个item定义一种状态(按下 选中 无焦点状态)

item下有shape标签,用来定义控件的显示效果,含有gradient,stoke,corners等子标签分别表示不同的效果。

在stoke圆角标签中,分别指定每个角的弧度时,bottomRightRadius代表左下角,bottomLeftRadius代表右下角。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值