搜尋此網誌

2014年10月18日 星期六

【Android】Textview刪除線實作

在TextView上加入刪除線

方法如下
TextView textview = (TextView) findViewById(R.id.textview1);
Paint paint = textview.getPaint();
paint.setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
paint.setAntiAlias(true);


轉回來
TextView textview = (TextView) findViewById(R.id.textview1);
Paint paint = textview.getPaint();
paint.setFlags(Paint.ANTI_ALIAS_FLAG);
paint.setAntiAlias(true);

參考網址:
http://jiun-blog.blogspot.tw/2012/06/android-textview-line-through.html

2014年10月17日 星期五

【Android】實作BaseAdapter

最常用的有幾種Adapter:
  • ArrayAdapter:將一組數組連繫到ListView
  • SimpleAdapter:適用於自訂ListView外觀的應用場合
  • BaseAdapter:抽象類別,所以有多個方法需要實作。適用於需要自訂ListView外觀等靈活應用的場合。
  • SimpleCursorAdapter:將一組現有的資料庫或聯絡人等ContentProvider的資料查詢的結果連繫到ListView中

顯示自訂的清單內容
ListView之一:Adapter介紹與使用
【Android】使用BaseAdapter自訂ListView
Android BaseAdapter提升效能的建議實作方式
Android適配器之ArrayAdapter、SimpleAdapter和BaseAdapter的簡單用法與有用代碼片段
ListView 與 BaseAdapter 的實作
Android - ListView + BaseAdapter 的應用
[Android]使用 BaseAdapter客製化 ListView
Android – Asynchronous image loading in ListView

2014年10月16日 星期四

【Android】EditText和Button放在同一行

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="15dp"
        android:text="Linear Layout with EditText and Button"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <EditText
            android:id="@+id/editText1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:hint="you can type here ..." />

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />
    </LinearLayout>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="15dp"
        android:text="Relative Layout with EditText and Button"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:text="Button" />

        <EditText
            android:id="@+id/editText2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_toLeftOf="@+id/button2"
            android:hint="you can type here ..." >
        </EditText>
    </RelativeLayout>

</LinearLayout>
轉載自 http://www.mysamplecode.com/2012/03/android-edittext-button-same-line.html

2014年10月15日 星期三

2014年10月11日 星期六

2014年10月10日 星期五

【Android】Intent.FLAG_ACTIVITY_CLEAR_TOP意思

http://developer.android.com/reference/android/content/Intent.html
If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.

照字面翻譯,將目前指定activity開啟,如果除指定外其他的activity是開啟的,則會被關閉。若指定的activity是已經開啟的,則不會重新開啟。

參考:
http://iskens.blogspot.tw/2011/01/android-activity.html

【Android】PendingIntent介紹和用法


  • Intent 是及時啟動的,intent隨activity消失而消失;PendingIntent可以看作是對intent的包裝。
  •  Intent 一般是用來作Activity、 Service、BroadcastReceiver之間傳遞數據,而Pendingintent,一般用在Notification上,可以理解為延遲執行的intent。


  • 簡單來說,Intent指定好要幹嘛後,就去做了。而PedingIntent則是先把某個Intent包好,丟給某個程式,以後再去執行Intent要幹嘛。比如用startActivity(intent)就會直接去啟動和intent關聯的某個程式了。相對的,假設APK1想告訴APK2在某個情況 下,去執行APK3的話,就可以在APK1設定好PendingIntent(內容就是去執行APK3),然後丟過去給APK2,然後APK2在某種情況 下,就去執行PendingIntent,那就會等於執行APK3。這裡不單單是指startActivity,也可以是對Broadcast和 Service進行處理。

  • 那為何不直接在APK2就用startActivity(Intent)寫好要執行 APK3,不就不用丟PendingIntent了?因為APK2要「動態」決定開啟,不同情況下要執行那一支APK,所以不能先寫死。假設使用 startActivity的情況下,如果有A1、B1、C1三支程式都要APK2去執行不同程式,分別為A3,B3和C3,那不就變成APK2的程式要 寫上startActivity(A3)、startActivity(B3)、startActivity(C3)。假設未來還會有新的D3、E3…會 被執行的話,APK2的程式碼,不就無法要不停的調整了。所以利用PendingIntent就可以解決這個問題。
以上節錄自:
http://developer.android.com/reference/android/app/PendingIntent.html
http://sunzone.iteye.com/blog/1998091
http://androidgill.blogspot.tw/2011/12/pendingintent1.html

2014年10月9日 星期四

【Android】Fragments簡介

適用於Android 3.0 (API level 11) and higher


http://jacky-google-android.blogspot.tw/2011/04/android-fragments.html
http://soldierzx0705.blogspot.tw/2013/07/preferencefragment.html

【Android】Class_Name.this的使用

在閱讀Android API Guide

時常看到Class_Name.this的使用

這個用法多用於在nested class中

當inner class必須使用到outer class的this instance時

舉例來說


class Outer{
    String data = "外部類別";

    public class Inner{
        String data = "內部類別";
        public String getOuterData(){
            return Outer.this.data;
        }
    }
}