android项目 之 来电管家(1) ----- 界面设计

因为需要,最近几天忙着写来电管家这个小软件,现在已经基本写的差不多了,基本的功能也都已实现,就剩下后续的完善了,而之前的记事本项目最近几天没写,但是肯定还是会完成的。

来电管家的基本功能,这里主要做的是拦截。

1.    添加黑白名单

2.    选择拦截模式

3.    启用拦截时间段

4.    拦截开关

主要功能,就是通过用户选择开启拦截,并选择拦截模式,这时就会启动后台监听服务,监听来电,判断是否挂断,并且,用户可以自由选择拦截时间段,也就是在该时间段内才启用监听服务。

先来看界面:

  

从界面其实也可以看出,这里主要应用了ActivityGroup和Activity的组合使用,以达到分页标签的作用,类似很多软件,如QQ。

直接上代码:

main.xml        这个就是ActivityGroup的布局文件,图中的四个页面就是四个Activity,就位于主布局文件中的LinearLayout容器中

<?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"
	>
	<RelativeLayout
	    android:layout_height="fill_parent"
		android:layout_width="fill_parent">
		<GridView
		    android:layout_height="35dp"
		    android:id="@+id/gvTopBar"
			android:layout_alignParentTop="true"
			android:layout_width="fill_parent"
			android:background="@drawable/tabbar_bg"
			android:layout_centerVertical="true"
			>
		</GridView>
		 <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/gvTopBar"
            android:src="@drawable/line"
            />
		<LinearLayout
			android:id="@+id/Container"
			android:layout_below="@+id/gvTopBar"
			android:layout_width="fill_parent"
			android:layout_height="fill_parent"
			android:orientation="vertical">
		</LinearLayout>

	</RelativeLayout>
</LinearLayout>

其中GridView用于放置顶部的分布菜单,ImageView就是图中的虚线,而最下面的LinearLayout则是放置四个页面的容器。

下面给出四个页面的布局文件

黑白名单页面布局文件  activity_add.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/layout"
    >

        <ListView 

        android:id="@+id/lv_show"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:cacheColorHint="#FFF" >

        </ListView>

     <RelativeLayout 

        android:id="@+id/add_layout"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:visibility="visible"
        android:background="@drawable/tabbar_bg"
        >
       <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/line"
            />
       <Button
        android:id="@+id/btn_add"
        android:layout_width="45dp"
        android:layout_height="45dp"
        android:layout_centerHorizontal="true"
        android:background="@drawable/add"
        />

        </RelativeLayout>
    <RelativeLayout 

        android:id="@+id/delete_layout"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:visibility="gone"
        android:background="@drawable/tabbar_bg"
        >
       <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/line"
            />
        <Button
            android:id="@+id/btn_cancel"
            android:layout_width="45dp"
            android:layout_height="45dp"
            android:background="@drawable/backup"
            />

        <TextView
            android:id="@+id/tv_select"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="共选择了项"
            android:layout_centerInParent="true"
            />
        <Button
            android:id="@+id/btn_delete"
            android:layout_width="45dp"
            android:layout_height="45dp"
            android:background="@drawable/delete"
            android:layout_alignParentRight="true"
            />
      </RelativeLayout>
</LinearLayout>

拦截记录的布局文件activity_callInfo.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

  <ListView 

        android:id="@+id/lv_show_callInfo"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
         >

        </ListView>

  <RelativeLayout 

        android:id="@+id/add_layout"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:visibility="visible"
        android:background="@drawable/tabbar_bg"
        >
       <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/line"
            />
       <Button
        android:id="@+id/btn_delete_callInfo"
        android:layout_width="45dp"
        android:layout_height="45dp"
        android:layout_centerHorizontal="true"
        android:background="@drawable/delete"
        />

    </RelativeLayout>

</RelativeLayout>

设置页面的布局文件activity_setting.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >
	<RelativeLayout
	    android:layout_width="match_parent"
	    android:layout_height="40dp"
	    android:gravity="center"
	    android:padding="8dp"
	    android:layout_marginLeft="20dp"
	    android:layout_marginRight="20dp"
	    android:layout_marginTop="30dp"
	    android:background="@drawable/setting_top"
	    >
	    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="开启监听"
        android:textSize="15sp"
        />
		<ToggleButton
		android:id="@+id/tb_switch"
	    android:layout_width="44dp"
        android:layout_height="20dp"
        android:background="@drawable/start_service_off"
        android:textOff=""
        android:textOn=""
        android:layout_alignParentRight="true"
	    />
	</RelativeLayout>

	<RelativeLayout
	    android:layout_width="match_parent"
	    android:layout_height="40dp"
	    android:gravity="center"
	    android:padding="8dp"
	    android:layout_marginLeft="20dp"
	    android:layout_marginRight="20dp"
	    android:background="@drawable/setting_middle"
	    >
	    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="白名单模式"
        android:textSize="15sp"
        />
		<ToggleButton
		android:id="@+id/tb_whitelist"
	    android:layout_width="44dp"
        android:layout_height="20dp"
        android:background="@drawable/start_service_off"
        android:textOff=""
        android:textOn=""
        android:layout_alignParentRight="true"
	    />
	</RelativeLayout>

	<RelativeLayout
	    android:layout_width="match_parent"
	    android:layout_height="40dp"
	    android:gravity="center"
	    android:padding="8dp"
	    android:layout_marginLeft="20dp"
	    android:layout_marginRight="20dp"
	    android:background="@drawable/setting_middle"
	    >
	    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="启用时间段"
        android:textSize="15sp"
        />
		<ToggleButton
		android:id="@+id/tb_time"
	    android:layout_width="44dp"
        android:layout_height="20dp"
        android:background="@drawable/start_service_off"
        android:textOff=""
        android:textOn=""
        android:layout_alignParentRight="true"
	    />
	</RelativeLayout>
	<RelativeLayout
	    android:id="@+id/start_layout"
	    android:layout_width="match_parent"
	    android:layout_height="40dp"
	    android:gravity="center"
	    android:padding="8dp"
	    android:layout_marginLeft="20dp"
	    android:layout_marginRight="20dp"
	    android:background="@drawable/setting_middle"
	    >
	<TextView
	    android:id="@+id/tv_start_tip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="开始时间 "
        android:textSize="15sp"
        android:textColor="@android:color/darker_gray"
        />
	<TextView
	    android:id="@+id/tv_start_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="2014-9-11   >"
        android:textSize="15sp"
        android:layout_alignParentRight="true"
        android:textColor="@android:color/darker_gray"
        />
	</RelativeLayout>
	<RelativeLayout
	    android:id="@+id/end_layout"
	    android:layout_width="match_parent"
	    android:layout_height="40dp"
	    android:gravity="center"
	    android:padding="8dp"
	    android:layout_marginLeft="20dp"
	    android:layout_marginRight="20dp"
	    android:background="@drawable/setting_under"
	    >
	<TextView
	    android:id="@+id/tv_end_tip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="结束时间 "
        android:textSize="15sp"
        android:textColor="@android:color/darker_gray"
        />
	<TextView
	    android:id="@+id/tv_end_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="2014-9-11   >"
        android:textSize="15sp"
        android:layout_alignParentRight="true"
        android:textColor="@android:color/darker_gray"
        />
	</RelativeLayout>

</LinearLayout>

图中设置页面中的四周包围的线,其实是用到了.9.png图片。

布局文件就上面这几个,那么顶部的分页菜单,及如何将四个Activity添加进ActivityGroup中。

1)顶部菜单的实现:

既然是GridView,当然要用适配器了,这里用的是自定义BaseAdapter,如下:

package com.example.callmanager;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;

public class TabBarAdapter extends BaseAdapter {
	private Context mContext;
	private TextView[] txtItems;
	private int selResId;
    public TabBarAdapter(Context c,int[] tabMenuId,int width,int height,int selResId) {
        mContext = c;
        this.selResId=selResId;
        txtItems=new TextView[tabMenuId.length];
        for(int i=0;i<tabMenuId.length;i++)
        {

        	txtItems[i] = new TextView(mContext);
        	txtItems[i].setLayoutParams(new GridView.LayoutParams(width, GridView.LayoutParams.WRAP_CONTENT));//设置ImageView宽高
        	txtItems[i].setGravity(Gravity.CENTER);
        	txtItems[i].setPadding(2, 5, 2, 5);
        	txtItems[i].setText(tabMenuId[i]);
        	txtItems[i].setTextSize(15);

        }
    } 

    public int getCount() {
        return txtItems.length;
    } 

    public Object getItem(int position) {
        return position;
    } 

    public long getItemId(int position) {
        return position;
    } 

    /**
     * 设置选中的效果
     */
    public void SetFocus(int index)
    {
        for(int i=0;i<txtItems.length;i++)
        {
            if(i!=index)
            {
            	txtItems[i].setBackgroundResource(0);//恢复未选中的样式

            }
        }
        txtItems[index].setBackgroundResource(selResId);//设置选中的样式

    }  

    public View getView(int position, View convertView, ViewGroup parent) {
        TextView textView;
        if (convertView == null) {
        	textView = txtItems[position];

        } else {
        	textView = (TextView) convertView;
        }
        return textView;
    }
} 

接下来就是将自定义的Adapter与GridView进行绑定了,主要代码如下(位于ActivityGroupDemo.java中):

	public GridView gvTopBar;
	private TabBarAdapter topImgAdapter;
	/** 顶部菜单 **/<br abp="804" /> 	int[] topbar_menu_array = { R.string.black_list,R.string.white_list,<br abp="805" />   	R.string.call_info,<br abp="806" />   	R.string.setting };
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		gvTopBar = (GridView) this.findViewById(R.id.gvTopBar);
		gvTopBar.setNumColumns(topbar_menu_array.length);// 设置每行列数
		gvTopBar.setSelector(new ColorDrawable(Color.TRANSPARENT));// 选中的时候为透明色
		gvTopBar.setGravity(Gravity.CENTER);// 位置居中

		int width = this.getWindowManager().getDefaultDisplay().getWidth()
					/ topbar_menu_array.length;
		topImgAdapter = new TabBarAdapter(this, topbar_menu_array, width, 48,
				R.drawable.menu_background);
		gvTopBar.setAdapter(topImgAdapter);// 设置菜单Adapter
	}
	

这样,顶部菜单的功能就已完成,并且,你点击某个菜单时,会有选中时的效果,如图中所示,这里同样使用的是.9.png图片。

2)将四个Activity添加进ActivityGroup,并点击相应的分布标签,进入对应的activity,四个Activity的代码不用多说,就是加载布局文件而已,主要看activityGroup中代码:

	public LinearLayout container;// 装载sub Activity的容器
	<pre class="java" name="code" abp="813">	gvTopBar.setOnItemClickListener(new ItemClickEvent());// 项目点击事件
	container = (LinearLayout) findViewById(R.id.Container);
    	SwitchActivity(0);//默认打开第0页

	class ItemClickEvent implements OnItemClickListener {

		@SuppressLint("NewApi") public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			SwitchActivity(arg2);

		}
	}
	/**
	 * 根据ID打开指定的Activity
	 * @param id GridView选中项的序号
	 */
	void SwitchActivity(int id)
	{
		topImgAdapter.SetFocus(id);//选中项获得高亮
		container.removeAllViews();//必须先清除容器中所有的View
		Intent intent =null;
		if (id == 0 )
			intent = new Intent(ActivityGroupDemo.this, BlackListActivity.class);

		else if (id == 1)
			intent = new Intent(ActivityGroupDemo.this,WhiteListActivity.class);
		else if (id == 2)
			intent = new Intent(ActivityGroupDemo.this, CallInfo.class);
		else if (id == 3)
			intent = new Intent(ActivityGroupDemo.this, SettingActivity.class);

		intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		//Activity 转为 View
		Window subActivity = getLocalActivityManager().startActivity(
				"subActivity", intent);
		//容器添加View
		container.addView(subActivity.getDecorView(),
				LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
	}
	

其实,就是利用Intent跳转页面,并将viiew添加进窗口中。

至此,界面设计部分已完成。

时间: 2024-08-03 15:31:34

android项目 之 来电管家(1) ----- 界面设计的相关文章

android项目 之 来电管家(2) ----- ListView+CheckBox的使用

上一节,已经完成了来电管家的界面设计,那么下面就要实现具体的功能了,如何将添加的黑白名单显示呢?这里用到了ListView,那么,如果需要删除黑白名单呢,是一个个长按弹出菜单删除,还是将所的黑白名单清空呢,这都不符合用户的需求,往往,都是删除多个,这就有个问题了,如何在ListView中删除指定的多个item呢??可能大家想到了,要用到CheckBox. 先看图: 可以看出,当处于删除模式时,底部按钮也变成了删除与返回,中间也显示了当前共选择了多少项,而且在ListView的每一个Item右边也

android项目 之 来电管家(5) ----- 添加监听服务

上面四节,已经实现了界面设计,黑名单(白名单的添加与删除与黑名单同理,不再赘述),启用监听开关,定时拦截开关,及拦截模式选择等功能,下面就要实现来电管家最核心的功能,也就是拦截功能.   主要思路: 1.  制定拦截模式,这里主要有两个,一个是黑名单模式,也就是只拦截在黑名单中的号码:另一个就是白名单模式,拦截除了白名单以外的号码. 2.  根据用户自定义设置信息,制定正则表达式,判断拦截的标准. 3.  使用对应的类实现挂断电话的功能. 监听模块主要是利用service实现功能,为了实现电话挂

android项目 之 来电管家(8) ----- 添加开机自启动监听服务

现在大多数的应用都会开机自启动,来电管家更是如此,添加了开机自启动监听服务后,开机后即使你没有打开来电管家应用程序,一样可以拦截来电信息. 开机自启动Activity或Service的方法: 主要步骤: 1.  要有开机要启动的service或activity(这里开机要启动的当然就是ListenService了) 2. 编写一个BroadcastReceiver用以捕获ACTION_BOOT_COMPLETED这条广播,并在捕获之后启动我们要启动的Activity或service. BootC

android项目 之 来电管家(7) ----- 加载用户设置并启监听用服务

因为我们用的是SharedPreferences来存储用户设置信息,那么在初次进入来电管家时,会有一些默认的设置,后续根据用户的设置,更新设置信息文件的内容. 打开应用程序,首先启动的是ActivityGroupDemo,并默认显示黑名单界面,这时,需要在ActivityGroupDemo.java中添加代码,判断用户是否是第一次进入该应用,如果是第一次,则写入默认的设置信息. 这里判断用户是否为第一次进入该应用程序,采用的思路为: 1)因为SharedPreferences会在应用程序目录下的

android项目 之 来电管家(4) ----- 添加拦截时间段

在大多数的骚扰拦截类的软件中都会有定时拦截这个实用的的功能,其实,也不难实现. 看图: 在未选中启用时间段时,下面的两个开始时间和结束时间呈灰色状态,并且单击无响应,启用时间段后,下面则变成了可以单击的状态,通过单击可以弹出选择日期与时间的对话框,用于选择开始时间和结束时间. 主要思路: 1.  启用时间段,将下面的控件变成可获得焦点状态 2.  添加单击事件,弹出日期时间选择对话框 3.  选择时间,并显示在相应的位置 4.  通过SharedPrefresence将相关设置保存,以便下次启动

android项目 之 来电管家(3) ----- 添加与删除黑名单

现在就实现具体的功能-----添加黑名单 先看图: 从图中也可以看出整个逻辑,就是: 1.  点击底部的添加按钮 2.  转到联系人选择界面选择联系人(这里调用的是系统的联系人界面,每次只能选择一个联系人,当然了,要实现每次选择多个联系人也可以,可以自定义选择联系人界面) 3. 返回选择的联系人并插入到数据库中. 4. 遍历数据库中的黑名单表,将所有的黑名单显示在ListView中 5. 删除联系人 6. 刷新黑名单显示列表 主要代码(BlackListActivity.java): priva

Android之十二微信UI界面设计

Android之十二微信UI界面设计 corners_bg.xml <span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color=&qu

Android开发1:基本UI界面设计——布局和组件

前言 啦啦啦~本学期要开始学习Android开发啦~ 博主在开始学习前是完完全全的小白,只有在平时完成老师要求的实验的过程中一步一步学习~从此篇博文起,博主将开始发布Android开发有关的博文,希望能在学习中和各位共同探讨,一起交流,共同进步~ 话不多说,首先进入我们的正题~Android开发一基本UI界面设计——布局和组件(Android Studio的配置安装使用等在以后为各位补上~) 基础知识 Android的组件分为布局和控件.布局,就是让控件在里面按一定的次序排列好的一种组件,本身并

Android 项目重构之路:界面篇

在前一篇文章<Android项目重构之路:架构篇>中已经简单说明了项目的架构,将项目分为了四个层级:模型层.接口层.核心层.界面层.其中,最上层的界面,是变化最频繁的一个层面,也是最复杂最容易出问题的一个层面,如果规划不好,很容易做着做着,又乱成一团了. 要规划好界面层,至少应该遵循几条基本的原则: 保持规范性:定义好开发规范,包括书写规范.命名规范.注释规范等,并按照规范严格执行: 保持单一性:布局就只做布局,内容就只做内容,各自分离好:每个方法.每个类,也只做一件事情: 保持简洁性:保持代