第一章:初入Android大門(程序加載應用)

效果:


[img]http://dl.iteye.com/upload/attachment/384581/2a432d77-a9c8-33b7-b0ee-f9973e5d4dbc.jpg[/img]


[img]http://dl.iteye.com/upload/attachment/384583/700df879-3086-3058-91ea-460fa4ca2fe0.jpg[/img]

main.xml


<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="@+id/widget0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>

<Button
android:id="@+id/loading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="這是個加載按鈕"
android:layout_x="97px"
android:layout_y="187px"
>
</Button>
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="24px"
android:text="TextView"
android:layout_x="116px"
android:layout_y="128px"
>
</TextView>

</AbsoluteLayout>



strings.xml


<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, LoadingTest!</string>
<string name="dialog_title">請稍等片刻..</string>
<string name="dialog_messge">加載中...</string>
<string name="app_name">LoadingTest</string>
</resources>




package londing.test;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class LoadingTest extends Activity {
/** 在Android裏是通過ProgressDialog來運行加載對話框,但留意的是 Android的ProgressDialog必須在
*後臺程序運行完畢之前以dismiss()方法來關閉取得(focus)焦點,否則成尋會陷入無限循環無法終止程序,
*或者在線程裏不可有任何的更改Context或paren View的任何狀態,文字輸出等事件。*/
private TextView text=null;
/**聲明一個ProgressDialog*/
public ProgressDialog myDialog=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/**載入main.xml*/
setContentView(R.layout.main);
/**通過ID找到Button組件*/
Button bt=(Button)findViewById(R.id.loading);
/**設置Button按鈕點擊事件*/
bt.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
/**設置ProgressDialog標題文字*/
final CharSequence title=getString(R.string.dialog_title);
/**設置ProgressDialog消息文字*/
final CharSequence messge=getString(R.string.dialog_messge);
/**設置ProgressDialog消息,標題文字顯示*/
myDialog=ProgressDialog.show(LoadingTest.this, title, messge,true);
/**通過ID找到TextView組件*/
text=(TextView)findViewById(R.id.text);
/**設置TextView文字*/
text.setText(messge);
new Thread(){
public void run(){
try{
/**線程睡眠*/
sleep(3000);
}catch(Exception e){
e.printStackTrace();
}finally{
/**關閉ProgressDialog*/
myDialog.dismiss();
}
}
/**開啓線程*/
}.start();
}
});
}
}


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