在AndroidApp應(yīng)用中,OnTouch事件表示觸摸事件,本章我們通過滑過圖像獲取當(dāng)前位置理解其具體用法。
成都創(chuàng)新互聯(lián)公司是一家集網(wǎng)站建設(shè),南華企業(yè)網(wǎng)站建設(shè),南華品牌網(wǎng)站建設(shè),網(wǎng)站定制,南華網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,南華網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。知識點(diǎn):OnTouch
一、設(shè)計(jì)界面
1、首先把c.jpg圖片復(fù)制到res/drawable-hdpi文件夾內(nèi)。
2、打開“res/layout/activity_main.xml”文件。
(1)從工具欄向activity拖出1個(gè)圖像ImageView、1個(gè)文本標(biāo)簽TextView。
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、界面如下:
二、長按事件
打開“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);
//注冊O(shè)nTouch監(jiān)聽器
ivwPicture.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// event.getX獲取X坐標(biāo);event.getY()獲?。僮鴺?biāo)
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;
}
}
在圖片上不斷滑動(dòng),則會顯示其不同的坐標(biāo)位置。
效果如下:
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
網(wǎng)站題目:OnTouchListener觸摸事件-創(chuàng)新互聯(lián)
本文路徑:http://www.rwnh.cn/article38/ceggpp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營銷推廣、關(guān)鍵詞優(yōu)化、品牌網(wǎng)站建設(shè)、品牌網(wǎng)站設(shè)計(jì)、自適應(yīng)網(wǎng)站、營銷型網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容