点击Android主菜单应用,直接进入Dialog对话框
来源:互联网 发布:人工智能上市公司一览 编辑:程序博客网 时间:2024/06/09 20:15
在Android系统中,通常我们打开打开一个对话框,都是在进入应用后,点击某一个按钮或者列表项进入Dialog对话框的,现在我们来写一个直接从点击应用后进入Dialog对话框。
设计思路跟我们点击一个按钮来打开对话框类似,只不过在其中我们屏蔽掉这一段的交互代码。点击应用后,创建Activity,执行onCreate()方法,然后在onCreate()方面内直接创建Dialog对话框就可以了。
需要注意的是,虽然这样设计之后可以达到需求,但是它后台毕竟还是有我们创建的Activity,所以我们必须屏蔽掉这个Activity所带来的一些页面上的痕迹,比如 Activity 的 label属性等。
1. 创建一个Activity,如下
package com.car.settings;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import android.app.Activity;import android.app.AlertDialog;import android.app.AlertDialog.Builder;import android.app.Dialog;import android.content.DialogInterface;import android.content.DialogInterface.OnKeyListener;import android.os.Bundle;import android.view.KeyEvent;import android.view.View;import android.view.Window;import android.widget.Button;import android.widget.SimpleAdapter;public class CarSettingsActivity extends Activity{/** Called when the activity is first created. */final int LIST_DIALOG = 0x113;// define the Setting menu name.private String[] names = new String[] { "Setting_item_1", "Setting_item_2","Setting_item_3" };// define the Setting menu iconprivate int[] imageIds = new int[] { R.drawable.ic_settings,R.drawable.ic_settings, R.drawable.ic_settings };//直接点击主菜单应用,进入Dialog@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE); showDialog(LIST_DIALOG);//屏蔽掉button按钮/*setContentView(R.layout.main);Button button = (Button) findViewById(R.id.button);button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {}});*/}@Overrideprotected Dialog onCreateDialog(int id, Bundle args) {switch (id) {case LIST_DIALOG:Builder b = new AlertDialog.Builder(this);b.setIcon(R.drawable.ic_launcher);b.setTitle("设备设置");List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>();for (int i = 0; i < names.length; i++) {Map<String, Object> listItem = new HashMap<String, Object>();listItem.put("icon", imageIds[i]);listItem.put("name", names[i]);listItems.add(listItem);}SimpleAdapter simpleAdapter = new SimpleAdapter(this, listItems,R.layout.settings_dialog, new String[] { "name", "icon" },new int[] { R.id.name, R.id.icon});b.setAdapter(simpleAdapter, new DialogInterface.OnClickListener() {//注意这边finish()掉的原因是前面 屏蔽掉了Activity后,在关闭Dialog后依然有它的痕迹,为了页面效果良好,直接 finish 掉@Overridepublic void onClick(DialogInterface dialog, int which) {finish();}});b.setOnKeyListener(new OnKeyListener() {//处理 Android 4.0 Dialog关闭掉后,点击返回键,Activity痕迹依然存在.@Overridepublic boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {// TODO Auto-generated method stubif(keyCode == KeyEvent.KEYCODE_BACK){finish();return true;}return false;}});return b.create();}return null;}}2. 由于main布局文件被屏蔽了,所以就不贴出来了。自定义一个Dialog的对话框:settings_dialog.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" > <!-- icon for settings menu --> <ImageView android:id="@+id/icon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="20dp"/> <!-- name for settings menu --> <TextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="16dp" android:gravity="center_vertical" android:paddingLeft="10dp" /></LinearLayout>3. 修改 AndroidManifest.xml文件,注意 android:theme="@android:style/Theme.Dialog"这个是关键。
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.car.settings" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="15" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".CarSettingsActivity" android:theme="@android:style/Theme.Dialog"> <intent-filter > <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application></manifest>4. 编译执行结果截图如下。
- 点击Android主菜单应用,直接进入Dialog对话框
- Android APP安装后直接打开,点击MENU进入系统主菜单,APP无法再次启动的问题
- android,点击返回按钮回到主菜单
- launcher界面按中间键直接进入主菜单实现方法
- Android 自定义主菜单
- Android对话框dialog大全(代码直接可用)
- 如何在Dialog上加载主菜单
- Android Dialog点击按钮不关闭对话框
- 封装好的Android对话框Dialog,包含了丰富的dialog样式、点击事件数据回传接口。直接调用封装好的方法即可操作dialog
- android 返回主菜单 Home
- Android 程序主菜单 布局
- Android之动画主菜单
- android 对话框Dialog和AlertDialog应用
- Android点击链接进入应用市场下载
- Android-Dialog对话框 全解(普通对话框,单选对话框,多选对话框,列表对话框,自定义对话框,Activity对话框)
- android Dialog的确定按钮点击后不取消对话框
- android Dialog的确定按钮点击后不取消对话框
- Android Dialog 点击对话框外部区域不关闭的设置
- 遇到的eclipse的问题
- 【框架解析】Hadoop系统分析(十)--对象序列化
- 运动是最佳健康良药
- 2013年手机浏览器如何站着把钱挣了?
- 最小矩阵差——有上下界的网络流问题
- 点击Android主菜单应用,直接进入Dialog对话框
- 获得选中的checkbox的value
- JS中void(0)的含义
- MySQLReport分析报告四
- This Handler class should be static or leaks might occur
- lucene搜索小例子
- Android的线程使用来更新UI----Thread、Handler、Looper、TimerTask等
- 关于C++组合理解以及拷贝构造函数的调用时间的理解
- js匿名函数的执行