Android點擊效果,android點擊

來源:互聯網
上載者:User

Android點擊效果,android點擊

  我們在開發網站時,會發現當我們添加<a/>標籤後,標籤有一個點擊效果,比如顏色變化,這樣開看起來使用者體驗會很棒,那麼在我們的Android開發中如何加入這樣的效果呢?本篇就為大家揭開它的神秘面紗。

  既然是點擊事件的變化,我們就要監控使用者是否點擊,這裡我有兩種實現方式:1、通過改變背景圖片達到上述效果;2、通過改變背景顏色來達到上述效果。

  兩者各有千秋,比如當我們設計按鈕(Button)的點擊事件時,建議使用第一種;而當我們設計諸如Item時,建議使用第二種。好了下面開始為大家進行程式碼分析:

  首先為大家介紹第一種:通過改變背景圖片來達到上述效果,既然是通過改變背景圖片,所以我們需要事先準備好2張圖片,一張作為未點擊時,一張作為點擊時。

  然後我們需要在res下建立一個drawable檔案夾,在裡面建立一個btn_bg.xml檔案:

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <!-- 點擊時  -->    <item android:drawable="@drawable/img_1" android:state_pressed="true"/>    <!-- 未點擊時 -->    <item android:drawable="@drawable/img_2" android:state_pressed="false"/></selector>

  我們的布局檔案(main_activity.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"    tools:context=".MainActivity" >        <Button         android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:background="@drawable/btn_bg"        android:text="點擊變化"        />    </RelativeLayout>

  特別注意紅色標註處。好了我們的第一個效果就實現了,大家感覺如何。下面我們來一起學習一下第二種方法:

  第二種方法無需準備圖片,我們通過改變背景的顏色來達到上述效果,既然這裡需要使用到顏色,先為大家分享一下16進位的顏色對照表:

  有了顏色對照表,我們就開始進行代碼解析吧:

  第一步在value檔案夾下建立一個col.xml檔案,把我麼可能使用到的顏色添加進去:

<?xml version="1.0" encoding="utf-8"?><resources>     <drawable name="write">#fff</drawable>     <drawable name="gray">#aaa</drawable> </resources> 

  第二步修改res下drawable檔案夾下的btn_bg.xml檔案:

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <!-- 點擊時  -->    <item android:drawable="@drawable/gray" android:state_pressed="true"/>    <!-- 未點擊時 -->    <item android:drawable="@drawable/write" android:state_pressed="false"/></selector>

  最後一步我們Item標籤設定:

<RelativeLayout             android:id="@+id/relative1"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:background="@drawable/btn_bg">            <ImageView                android:id="@+id/img1"               android:layout_width="50dp"               android:layout_height="50dp"               android:src="@drawable/img_1"               android:layout_marginLeft="20dp"               android:layout_marginTop="20dp"               android:layout_centerVertical="true"                />            <TextView                 android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_marginLeft="20dp"                android:textColor="#fff"                android:text="第一個item"                android:layout_toRightOf="@id/img1"                android:layout_centerVertical="true"                />       </RelativeLayout>

  這樣我們Item也添加上了點擊事件變化,最後總結一下這兩種方法,第一種方法我們需要事先準備材料,而第二種方法,相對來說就靈活許多,我們可以隨心所欲的進行修改,非常的方便。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.