1. 程式人生 > >android Button按下及擡起監聽事件

android Button按下及擡起監聽事件

首先我們在 佈局中,寫入Button按鈕

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="test.bwie.com.wzq_20170921day20.MainActivity"
    >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btn"
        android:layout_gravity="center"
        android:text="點我有驚喜"/>

</LinearLayout>

在MainActivity 中使用 OnTouchListener 的監聽事件
package test.bwie.com.wzq_20170921day20;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn = (Button) findViewById(R.id.btn);
        //監聽
        btn.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction() == MotionEvent.ACTION_DOWN){
                    Toast.makeText(MainActivity.this,"按下了" ,Toast.LENGTH_SHORT).show();
                    btn.setBackgroundColor(Color.RED);
                }else if(event.getAction() == MotionEvent.ACTION_UP){
                    Toast.makeText(MainActivity.this,"鬆開了" ,Toast.LENGTH_SHORT).show();
                    btn.setBackgroundColor(Color.BLUE);
                }
                return false;
            }
        });

    }
}


相關推薦

android Button事件

首先我們在 佈局中,寫入Button按鈕 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android

Unity3D---UGUI---Button相關、點選、、長事件

一、先介紹一下Inspector面板裡的東西 Button裡一共有兩個指令碼。 image.png Image(Script) 這個跟Image裡是一樣的 Button(Script) Interactable(互動):Button是否可以點

[MFC]MFC的BUTTON控制元件響應滑鼠

有效方法: BOOL CButtontestDlg::PreTranslateMessage(MSG* pMsg) { // TODO: Add your specialized code here and/or call the base class if (pMsg-&g

最好的按鍵掃描和消抖方法,適用於復合、長響應按鍵

按鍵消抖 按鍵掃描 C語言按鍵 剛參加工作的時候,看了一些同事采用的按鍵掃描和消抖方法,對比學校裏和網上查到的按鍵處理,發現覺得不盡善盡美,有以下幾點: 1. 消抖復雜,效率低。有人直接在電平判斷後使用delay()函數,進行消抖,耽誤時間;有人在按鍵電平中斷中進行消抖和處理,導致其他的服務反應慢

VC++中獨立捕獲按鈕兩個動作

BOOL CRealButtonDlg::OnWndMsg(UINT message, WPARAM wParam, LPARAM lParam, LRESULT* pResult) { switch(message) { case 9999: if(wParam == 1) // 按鈕被滑鼠

模仿微視視訊錄製、支援錄製暫停以及斷點進度條(基於javacv)

/* * Copyright (C) 2009,2010,2011,2012,2013 Samuel Audet * * This file is part of JavaCV. * * JavaCV is free software: you can redistribute it and/or

Button一些屬性和幾種事件

介紹: TextView與Button是繼承關係,Button繼承自TextView。開發中一般情況下,TextView用來承載靜態文字,Button用來響應點選事件。很多人容易忽略的一點是TextVIew也有點選事件,TextView 繼承自View,點選事件是從View就有的特性,所以Tex

Android中自定義ScrollView的滑動事件

專案結構: 1.LazyScrollView類(自定義ScrollView) package android.zhh.com.myapplicationscrollview; /** * Created by sky on 2017/3/19. */ impor

Android按鈕的五種點選事件處理

1、匿名內部類作為監聽器 button.setOnClickListener(new OnClickListener() { @Overridepublic void onClick(View v) {// TODO Auto-generated method stub}}

Android中自定義ScrollView的滑動事件,並在滑動時漸變標題欄背景顏色

效果圖 滑動前: 滑動中: 滑動到底部: 專案結構 ObservableScrollView package com.jukopro.titlebarcolor; import android.content.Context; import android.u

Android屬性動畫欣賞——ObjectAnimator與動畫事件

傳統動畫的侷限 傳統動畫(Animation)只是重繪動畫(就是不斷的呼叫相應的方法重繪,會耗費CPU資源),改變其顯示位置,但是真正能響應事件的位置還在原處。因此不適宜做具有互動的動畫效果,僅僅用來做一些顯示性的效果。(包括位移、旋轉、縮放、透明度四種動畫)

android--ListView(控制元件+二種介面卡+事件)

介面卡就是大量資料來源顯示到檢視上 介面卡分為簡單介面卡和陣列介面卡 陣列介面卡:先在佈局檔案新建一個ListVi控制元件 1.新建一個數組介面卡 arrAdapter=new ArrAdapter<String>(this,android.layout.sim

android 開發button有震動怎樣實現

android 開發常有這樣的小需求:當Button按下時有震動,方法如下: 在按鈕按下事件onClick或者onTouch回撥中加入下面這段程式碼即可產生震動效果,震動的節奏由引數裡的long陣列來

Button按鈕的起點選事件

import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.MotionEvent; import android.view.View; imp

Android 按鍵實現陰影效果

nal turn view font .cn ges activity rda als final Button btn_gallery=(Button)findViewById(R.id.btn_gallery);btn_gallery.setOnClickListene

Android按鈕時和彈起時的顏色設定

(1)首先在res資料夾下新建一個資料夾drawable,這是無關解析度的,在下面建立一個xml檔案:button.xml <?xml version="1.0" encoding="utf

Android同時多個按鈕

關鍵字: 多點觸控, 控制元件, 按鈕, Android, 同時按下 【也許這樣能幫到更多人檢索到??】 很久沒寫博文了,最近在做畢業設計的過程中,碰到了這樣一個問題,之前一直沒遇到過,走了一些彎路,現在寫下來記錄下。 在百度的過程中並沒有看到有相關的解答,而更多的還是帶你走向彎路的

UCSDK黑屏問題:android遊戲home後,從任務管理進入遊戲黑屏問題

將接了sdk的遊戲安裝到android手機裡,點選HOME鍵,然後再回到遊戲中,發生黑屏現象。接應用寶的sdk時:點選支付按鈕,獲得支付彈窗後,按home鍵回到桌面,再從任務管理那裡進入遊戲,只能看到支

Android EditText輸入法軟體的enter按鍵,實現跳到一個EditText等

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height=

android button雙擊事件

第一次做按鈕雙擊監聽事件,在這裡就分享出來我自己的做法 剛開始我直接這樣做的: private int count = 0; button.setOnClickListener(new OnClick