Android Dialog說明

Download Report

Transcript Android Dialog說明

第四章
手機與PDA行動裝置平台簡介
實驗介紹
•實驗目的
–撰寫程式 “EX03_12”
–示範常用於「程式提示」、 「警告」或、
「確認」等等的Dialog視窗。
–在Layout當中設計一個按鈕,當按下按鈕,即
產生AlertDialog對話視窗。
EX03_12
• 撰寫“EX03_12”程式
• 在模擬器中執行
• 情境:
–在Layout當中設計一個
按鈕,當按下按鈕,即
產生AlertDialog對話視窗。
介面 main.xml(1)
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation=“vertical“
//設定版面為垂直排列
android:layout_width="fill_parent"
//寬度填滿
android:layout_height="fill_parent“
//長度填滿
>
<TextView
//加入文字顯示物件
android:layout_width="fill_parent"
//設定TextView寬度
android:layout_height="wrap_content"
//設定TextView高度
android:text=“@string/hello”
//TextView顯示內容為讀取string.xml
的hello字串內容
/>
<Button
//放入按鈕物件
android:id="@+id/myButton1“
//設定此按鈕物件名稱為myButton1
android:layout_width="wrap_content“
//設定按鈕的寬度
android:layout_height="wrap_content"
//設定按鈕的高度
android:text=“@string/str_button1“
//設定按鈕顯示的文字為讀取string.xml的
str_button1字串內容
/>
</LinearLayout>
介面 mylayout.xml(2)
•
•
•
•
TextView
TextView物件
Button
Button物件
Android程式碼 EX03_12.java
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
Import略
public class EX03_12 extends Activity
{
private Button mButton1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mButton1 = (Button) findViewById(R.id.myButton1);
mButton1.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
new AlertDialog.Builder(EX03_12.this)
.setTitle(R.string.app_about)
.setMessage(R.string.app_about_msg)
.setPositiveButton(R.string.str_ok,
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialoginterface, int i)
{
}
}
)
.show();
}
});
}
}
• // TODO Auto-generated method stub
•
new AlertDialog.Builder(EX03_12.this)
•
.setTitle(R.string.app_about)
•
.setMessage(R.string.app_about_msg)
•
.setPositiveButton(R.string.str_ok,
•
new DialogInterface.OnClickListener()
•
{
•
public void onClick(DialogInterface dialoginterface, int i)
•
{
•
}
•
}
•
)
•
.show();