找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開(kāi)始

搜索
查看: 2939|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

安卓事件處理 顯示用戶觸摸持續(xù)時(shí)間的小程序源碼

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:856808 發(fā)表于 2020-12-15 20:14 | 只看該作者 |只看大圖 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
本帖最后由 dori 于 2020-12-18 23:13 編輯

設(shè)計(jì)一個(gè)顯示用戶觸摸持續(xù)時(shí)間的小程序

運(yùn)行成功:
                              

MainActivity.java文件:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.view.MotionEvent;
import android.view.View.OnTouchListener;

public class MainActivity extends Activity implements OnTouchListener
{
    private ImageView iv;
    private EditText et;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //加載布局
        setContentView(R.layout.activity_main);
        iv=findViewById(R.id.iv);
        et=findViewById(R.id.et);
        iv.setOnTouchListener(this);
    }
    @Override
    public boolean onTouch(View v,MotionEvent event){
        // TODO Auto-generated method stub
        long time=event.getEventTime()-event.getDownTime();//計(jì)算觸摸持續(xù)時(shí)間
        String s = String.valueOf(time);
        if(event.getActionMasked()==MotionEvent.ACTION_UP){
            et.setText(s);
        }
        return true;
    }
}


activity_main.xml文件:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:text="觸摸時(shí)間:"/>

        <EditText
            android:id="@+id/et"
            android:layout_width="200sp"
            android:layout_height="wrap_content"
            android:textColor="@color/colorAccent" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="毫秒"/>
    </LinearLayout>
    <ImageView
        android:id="@+id/iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/background"/>
</LinearLayout>

事件處理.rar (10.38 MB, 下載次數(shù): 3)

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表