安卓實習第五天

1.EditText不彈出軟鍵盤直接跳轉到另一個Activity

android:focusableInTouchMode="false"

2.修改頁面跳轉的切換效果

從一個 Activity 跳轉到另一個 Activity 通常是通過 Intent 和 startActivity() 方法來切換頁面,Android 提供一個方法叫做 overridePendingTransition(int enterAnim, int exitAnim),通過這個方法可以重寫 Activity 的過渡/切換動畫,但這個方法一般教程都說要緊隨 startActivity 方法或者 Activity 的 finish 方法調用。所以一般的方法是:

在 Activity1中,startActivity 之後一行寫 overridePendingTransition(int enterAnim, int exitAnim),這樣確實能夠正常按照規定的動畫啓動 Activity2,但是 Activity2 退出的時候,並沒有調用我們設定的 exitAnim,所以我發現最好的方法是,在 Activity2 的 onCreat 方法中,在調用以下代碼之前

<span style="font-size:18px;">super.onCreate(bundle);</span>

寫上例如我這樣的代碼:

<span style="font-size:18px;">// 我設置了兩個動畫資源文件作爲啓動動畫和退出動畫
overridePendingTransition(R.anim.scale_in, R.anim.alpha_out);</span>

之後還不夠,還得重寫下 Activity2 的 finish() 方法:
<span style="font-size:18px;">@Override
publicvoid finish() {
    super.finish();
    // 仍然是這句,但不得不再寫一遍,否則退出的時候,會調用成 scale_in 的反向動畫,讀者可以做實驗試試
    overridePendingTransition(R.anim.scale_in, R.anim.alpha_out);
}</span>


這樣以後就大功告成了,你在 Activity1 中只要調用 startActivity 即可,不用緊接overridePendingTransition(int enterAnim, int exitAnim) 方法,即可完成想要的 Activity2 啓動動畫和退出動畫的設定。

至於,overridePendingTransition(int enterAnim, int exitAnim) 的參數,就是兩個資源文件,用 xml 文件來定義,我的是這樣的,直接給出參考,大家一看就懂。但首先你得在 res 文件夾下,建立一個文件夾叫做 anim,再在 anim 文件夾下面建立兩個 xml 文件,如下:

scale_in.xml

<span style="font-size:18px;"><?xmlversion="1.0"encoding="utf-8"?>
<setxmlns:android="http://schemas.android.com/apk/res/android">
    <scale
        android:interpolator="@android:anim/decelerate_interpolator"
        android:fromXScale="0.0"
        android:toXScale="1.0"
        android:fromYScale="0.0"
        android:toYScale="1.0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:startOffset="0"
        android:duration="200"
        android:fillAfter="true"
        android:fillEnabled="true"
        />
    <alpha
        android:fromAlpha="0.0"
        android:toAlpha="1.0"
        android:duration="200"
        />
</set></span>


alpha_out.xml

<span style="font-size:18px;"><?xmlversion="1.0"encoding="utf-8"?>
<setxmlns:android="http://schemas.android.com/apk/res/android">
    <alpha
        android:fromAlpha="1.0"
        android:toAlpha="0.0"
        android:duration="300"
        />
</set></span>


我只是教會你好的方法,更多 xml 定義的動畫,你可以參考:


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章