我的android系列學習筆記,只打算給自己看的,路過的客官如有參考實乃在下萬幸
這個Menu按鈕,當用戶使用時,會彈出菜單界面,例如
這個是版本很老的android的效果
就是按按鈕了以後,就會彈出菜單界面
AlterDialog的使用
就是這玩意,當我的項目點退出的時候,就會彈出警告,就是警告用的。
而實現它的步驟:
- 創建對話框的構造器
- AlertDialog.Bulider dl1=new AlertDialog.Builder(this)
- 通過構造器來設置各項的內容(如圖標、標題、內容),例如
- dl1.setTitle("***")
- dl1.setIcon(R.***)
- dl1.setMessage("****")
- 通過構造器來創建對話框
- 通過構造器來顯示對話框
接下來,就是通過一個小項目,來實現上述兩個功能
首先是佈局界面
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical"
android:background="@color/bg_color" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="@dimen/title_height"
android:text="@string/app_name"
android:textSize="@dimen/title_text_size"
android:textColor="@color/title_text_color" />
<Button
android:id="@+id/btn_about"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="@string/about_text"
android:textSize="@dimen/btn_text_size"
android:textColor="@color/btn_text_color" />
<Button
android:id="@+id/btn_continue"
android:layout_width="@dimen/btn_width"
android:layout_height="wrap_content"
android:text="@string/continue_text"
android:textSize="@dimen/btn_text_size"
android:textColor="@color/btn_text_color" />
<Button
android:id="@+id/btn_newgame"
android:layout_width="@dimen/btn_width"
android:layout_height="wrap_content"
android:text="@string/new_game_text"
android:textSize="@dimen/btn_text_size"
android:textColor="@color/btn_text_color" />
<Button
android:id="@+id/btn_exit"
android:layout_width="@dimen/btn_width"
android:layout_height="wrap_content"
android:text="@string/exit_text"
android:onClick="onClick"
android:textSize="@dimen/btn_text_size"
android:textColor="@color/btn_text_color" />
</LinearLayout>
然後是java代碼,具體來看。
public class GameMain extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game_main);
}
public void onClick(View view)
{
switch(view.getId())
{
case R.id.btn_exit:
isFinish(); //點的如果是退出按鈕,則跳轉到AlertDialog界面
break;
}
}
//設置AlertDialog界面,警告界面
public void isFinish()
{
//這個是創建一個構造器,名字是dl1
AlertDialog.Builder dl1 = new AlertDialog.Builder(this);
//接下來,是通過構造器來設定對話框的各項內容
dl1.setTitle("Warnning!"); //設置界面的標題
dl1.setMessage("Do you want exit?"); //設置界面的警告信息
dl1.setPositiveButton("yes", new DialogInterface.OnClickListener() {
//如果點的是積極的按鈕,第一個參數,yes,是那個按鈕的字,而第二個參數,就是監聽點擊此按鈕發生的操作
@Override
public void onClick(DialogInterface dialog, int which) {
GameMain.this.finish();
}
});
dl1.setNeutralButton("no", new DialogInterface.OnClickListener() {
//這個是中性的按鈕,不帶有肯定和否定意思的按鈕。同樣,第一個參數,是設置按鈕上邊顯示的字,第二個參數,則是監聽點擊此按鈕發生的操作
@Override
public void onClick(DialogInterface dialog, int which) {
// 在此,當用戶單擊cancle按鈕,不做任何動作
}
});
//創建對話器
dl1.create();
//顯示對話器
dl1.show();
}
//實例化菜單XML文件成菜單對象
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// 使用MenuInflater類來實例化菜單XML文件成菜單對象
MenuInflater inflater=new MenuInflater(this);
inflater.inflate(R.menu.game_menu, menu);
return super.onCreateOptionsMenu(menu);
}
//響應menu菜單的選項
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch(item.getItemId())
{
case R.id.music:
/**
* 此處使用意圖來傳遞信息完成不同類之間的跳轉
* 下一章節將詳細介紹Intent類,此處使用即可
* */
Intent intent1=new Intent(GameMain.this,Music.class);
startActivity(intent1);
break;
case R.id.help:
Intent intent2=new Intent(GameMain.this,Helper.class);
startActivity(intent2);
}
return super.onOptionsItemSelected(item);
}
}
就先這樣吧,還有什麼要加的以後想起來了再改