安卓开发之Button

来源:互联网 发布:415打击网络政治谣言 编辑:程序博客网 时间:2024/05/02 01:40

按钮按下、长按、松开三种状态:

切换背景图片时使用

setOnClickListener:按下

setOnLongClickListener:长按
setOnTouchListener:松开

bt1.setOnTouchListener(new View.OnTouchListener() 
{
   @Override
   public boolean onTouch(View view, MotionEvent motionEvent) 
   {
       //按下操作
       if(motionEvent.getAction()==MotionEvent.ACTION_DOWN)

       {

}
       //抬起操作
       if(motionEvent.getAction()==MotionEvent.ACTION_UP)
       {
       }
       //移动操作
       if(motionEvent.getAction()==MotionEvent.ACTION_MOVE){
            
       }
       return false;
   }
});


设置按钮边框、背景

GradientDrawable drawable = new GradientDrawable();  
drawable.setShape(GradientDrawable.RECTANGLE); // 画框  
drawable.setStroke(1, Color.BLACK); // 边框粗细及颜色 

drawable.setColor(Color.YELLOW);

bt1.setBackgroundDrawable(drawable); 

设置按钮字体背景

bt1.setBackgroundColor(0xFF62BCFA);

0 0