OnTouchListener触摸事件

在AndroidApp应用中,OnTouch事件表示触摸事件,本章我们通过滑过图像获取当前位置理解其具体用法。

创新互联专注为客户提供全方位的互联网综合服务,包含不限于成都网站设计、成都做网站、隆安网络推广、微信小程序定制开发、隆安网络营销、隆安企业策划、隆安品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联为所有大学生创业者提供隆安建站搭建服务,24小时服务热线:18982081108,官方网址:www.cdcxhl.com

知识点:OnTouch

一、设计界面

1、首先把c.jpg图片复制到res/drawable-hdpi文件夹内。

OnTouchListener触摸事件

2、打开“res/layout/activity_main.xml”文件。

(1)从工具栏向activity拖出1个图像ImageView、1个文本标签TextView。

OnTouchListener触摸事件

3、打开activity_main.xml文件。

 代码如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >


    <ImageView
        android:id="@+id/picture"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/c" />


    <TextView
        android:id="@+id/info"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/picture"
        android:text="@string/info" />


</RelativeLayout>

4、界面如下:

OnTouchListener触摸事件

二、长按事件 

 打开“src/com.genwoxue.onlongclick/MainActivity.java”文件。

 然后输入以下代码:  

package com.example.hw;


import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.TextView;




public class MainActivity extends Activity {
private ImageView ivwPicture = null;
private TextView tvInfo = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvInfo = (TextView) findViewById(R.id.info);
ivwPicture = (ImageView) findViewById(R.id.picture);
//注册OnTouch监听器
ivwPicture.setOnTouchListener(new OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
// event.getX获取X坐标;event.getY()获取Y坐标
String sInfo = "X="+String.valueOf(event.getX())+"  Y="+String.valueOf(event.getY());
tvInfo.setText(sInfo);
return true;
}
});
}


@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;
}
}

在图片上不断滑动,则会显示其不同的坐标位置。

 效果如下:

OnTouchListener触摸事件

标题名称:OnTouchListener触摸事件
本文路径:https://www.cdcxhl.com/article42/pgjiec.html

成都网站建设公司_创新互联,为您提供企业建站用户体验定制网站搜索引擎优化域名注册云服务器

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

网站优化排名