Android学习笔记3:Menu菜单以及AlterDialog的使用

我的android系列学习笔记,只打算给自己看的,路过的客官如有参考实乃在下万幸

这个Menu按钮,当用户使用时,会弹出菜单界面,例如

这个是版本很老的android的效果

就是按按钮了以后,就会弹出菜单界面

AlterDialog的使用

就是这玩意,当我的项目点退出的时候,就会弹出警告,就是警告用的。

而实现它的步骤:

  1. 创建对话框的构造器
    1. AlertDialog.Bulider dl1=new AlertDialog.Builder(this)
  2. 通过构造器来设置各项的内容(如图标、标题、内容),例如
    1. dl1.setTitle("***")
    2. dl1.setIcon(R.***)
    3. dl1.setMessage("****")
  3. 通过构造器来创建对话框
  4. 通过构造器来显示对话框

接下来,就是通过一个小项目,来实现上述两个功能

   首先是布局界面

<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);
	}
	    
}

 就先这样吧,还有什么要加的以后想起来了再改

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值