网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

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文件。

 代码如下:

    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" >


            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" />


            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" />


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触摸事件
标题链接:http://bjjierui.cn/article/pgjiec.html

其他资讯