<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # Android 中的事件處理 > 原文: [https://javatutorial.net/event-handing-android](https://javatutorial.net/event-handing-android) 在先前的教程中,您了解了[活動](https://javatutorial.net/android-activity-example),[意向](https://javatutorial.net/android-intent-example)和其他內容。 本教程借助示例介紹了 Android 中的事件處理。 ## 事件處理 事件是一個動作。 當用戶與移動應用程序交互時,會發生這種情況。 Android 中的事件采用多種不同形式,例如按鍵,觸摸輸入和許多其他形式。 觸摸屏交互屬于觸摸事件的類別。 Android 框架將所有事件維護在先進先出(FIFO)隊列中。 顧名思義,事件處理程序用于處理 Android 中的事件。 事件處理程序和事件監聽器是相關概念。 ## 事件監聽器 它是一個包含回調方法的接口。 當用戶觸發視圖元素時,它將調用這些方法。 以下是一些重要的回調方法: * `OnClickListener()`:當用戶單擊任何 UI 元素(如按鈕,文本或圖像)時,將調用此事件監聽器。`OnClick()`事件處理程序用于處理此監聽器。 * `OnLongClickListener()`:當用戶長時間單擊任何 UI 元素或按住 UI 元素幾秒鐘時,將調用此方法。`OnLongClick()`事件處理程序用于處理此監聽器。 * `OnFocusChangeListener()`:當 UI 元素或小部件失去焦點時,將調用此方法。 只需用戶向前導航。`OnFocusChange()`事件處理程序用于處理此監聽器。 * `OnKeyListener()`:當用戶按下鍵盤上的鍵時,將調用此方法。`OnKey()`事件處理程序用于處理此事件。 * `OnTouchListener()`:當用戶觸摸屏幕上的任何 UI 元素(例如按下或釋放按鈕)時,將調用此方法。`OnTouch()`事件處理程序用于處理此監聽器。 * `OnMenuItemClickListener()`:當用戶單擊或選擇菜單項時使用此方法。`OnMenuItemClick()`事件處理程序用于處理此監聽器。 * `OnCreateContextMenuListener()`:`OnCreateContextMenu()`事件處理程序用于處理此監聽器。 ## 事件處理示例 讓我們開始創建一個示例,說明如何在 Android 應用中使用事件處理程序和事件監聽器。 打開 Android Studio 并創建一個新項目。 打開您的`activtity_main.xml`并添加測試視圖,圖像視圖和按鈕。 這是代碼 ```java <?xml version="1.0" encoding="utf-8"?> <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" tools:context="MainActivity"> <TextView android:id="@+id/tv_welcome" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Welcome to " android:textColor="@android:color/holo_red_dark" android:textSize="40dp" android:layout_centerVertical="true" android:layout_centerHorizontal="true" /> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageButton" android:src="@drawable/message" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="20dp" /> <Button android:id="@+id/button" android:layout_width="150dp" android:layout_height="wrap_content" android:background="@android:color/holo_red_dark" android:text="Change Color" android:textColor="@android:color/background_light" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="70dp" /> <TextView android:id="@+id/tv_java" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@android:color/holo_red_dark" android:textSize="40dp" android:layout_marginTop="21dp" android:text="Java Tutorial.net" android:layout_below="@+id/tv_welcome" android:layout_centerHorizontal="true" /> </RelativeLayout> ``` 現在在`mainActivity.java`中編寫代碼,當用戶單擊按鈕時,文本的顏色將改變,在此實現`onClickListener()`。 最初,文本顏色為紅色,第一次單擊時顏色將變為黑色,第二次單擊時顏色將變為綠色,依此類推。 這是代碼 ```java package com.example.admin.androideventhandling; import android.graphics.Color; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends AppCompatActivity { TextView welcome; TextView java; Button button; int i=0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); welcome = (TextView)findViewById(R.id.tv_welcome); java = (TextView)findViewById(R.id.tv_java); button = (Button) findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(i==0) { welcome.setTextColor(Color.BLACK); java.setTextColor(Color.BLACK); button.setBackgroundColor(Color.BLACK); } if(i==1) { welcome.setTextColor(Color.GREEN); java.setTextColor(Color.GREEN); button.setBackgroundColor(Color.GREEN); } if(i==2) { welcome.setTextColor(Color.BLUE); java.setTextColor(Color.BLUE); button.setBackgroundColor(Color.BLUE); } if(i==3) { welcome.setTextColor(Color.MAGENTA); java.setTextColor(Color.MAGENTA); button.setBackgroundColor(Color.MAGENTA); i=0; } i++; } }); } } ``` 在這里運行您的應用程序,輸出結果看起來像 ![event handling](https://img.kancloud.cn/eb/c5/ebc5883067907d950254dcb68693eabb_363x639.jpg) 事件處理 單擊按鈕后,文本顏色將更改 ![event handling color change](https://img.kancloud.cn/c3/f2/c3f201adfa96764d4ed213875247ee60_366x636.jpg) 事件處理顏色變化 ![event handling example](https://img.kancloud.cn/be/f7/bef7f044433d1e1f9bc3613e71b28b19_370x652.jpg) 事件處理示例 ![event handling magenta color](https://img.kancloud.cn/e9/d2/e9d23498c9aaab69f0b7b2c75772ca6f_371x653.jpg) 事件處理:洋紅色 ![event handling blue color](https://img.kancloud.cn/cc/46/cc46bb8b9a1671995f0a9624b90088de_369x653.jpg) 事件處理:藍色 您可以通過單擊[鏈接](https://github.com/JavaTutorialNetwork/Tutorials/blob/master/AndroidEventHandling.rar)下載此代碼。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看