iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 17
4
Software Development

[Andriod] Andriod Studio 從入門到進入狀況系列 第 17

[Day 16] Android程式設計番外篇 - 元件觸發事件的四種方法(二)

今天分享的是第二種方法:透過implements來達到這樣的觸發事件功能,
跟第一種方法非常的類似,
但寫法有些不同.
直接貼程式碼吧,

這是activity_main.xml的內容

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    tools:context=".MainActivity">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="@dimen/activity_horizontal_margin"
            android:layout_marginTop="@dimen/activity_vertical_margin"
            android:src="@drawable/ic_launcher" />

    </RelativeLayout>
</android.support.constraint.ConstraintLayout>

這是MainActivity.java的內容

package com.example.user.test;

import android.app.Activity;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.View.OnTouchListener;
import android.view.KeyEvent;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnTouchListener {
    private ImageView myImageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //初始化控制項
        initViews();
        //初始化資料
        initData();
        //設定監聽事件
        setListeners();
    }

    private void initViews()
    {
        myImageView = (ImageView)findViewById(R.id.imageView1);
    }

    private void initData()
    {
    }

    private void setListeners()
    {
        //呼叫一個新的class
        myImageView.setOnTouchListener(this);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        ImageView t_v = (ImageView)v;
        if(t_v == myImageView)
        {
            Toast.makeText(getApplicationContext(), "您好!Android!", Toast.LENGTH_SHORT).show();
        }
        return false;
    }
}

畫面跟昨天一樣
https://ithelp.ithome.com.tw/upload/images/20181022/20105694VlcVP108vn.png

以下是簡單的說明:

這一種寫法的implements就直接寫在主程式MainActivity上面

public class MainActivity extends Activity implements OnTouchListener

這樣開發者就可以直接在主程式MainActivity上面直接使用

@Override
public boolean onTouch(View v, MotionEvent event)

就不用像第一種方法,必須另外創造一個class,直接透過同一個class使用就可以了。

當然還是要指定哪一個元件需要監聽事件

myImageView.setOnTouchListener(this);

上一篇
[Day 15] Android程式設計番外篇 - 元件觸發事件的四種方法(一)
下一篇
[Day 17] Android程式設計番外篇 - 元件觸發事件的四種方法(三)
系列文
[Andriod] Andriod Studio 從入門到進入狀況33
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言