一.搭建开发环境
1.所需资源
JDK6以上
Eclipse3.6以上
SDK17, 2.3.3
ADT17
2.安装注意事项
不要使用中文路径
如果模拟器默认路径包含中文, 可以设置android_sdk_home环境变量解决。效果如下:

二.拨打电话
1.步骤
在Button节点中添加onClick属性, 指定一个方法名
在Activity中定义一个public void 方法名 (View view)
获取文本框中的号码
创建意图, 设置动作, 设置数据
使用意图开启Activity
2.注意
必须声明权限: android.permission.CALL_PHONE, 否则拨打电话时会抛出异常
声明权限后, 安装软件时会有提示

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.shellway.phone.MainActivity" > <TextView
android:id="@+id/tv_phone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/input_num" /> <EditText
android:inputType="phone"
android:id="@+id/et_phone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/tv_phone"
android:ems="10" >
<requestFocus />
</EditText> <Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/et_phone"
android:text="@string/call" /> </RelativeLayout>

activity_main.xml:页面布局

<?xml version="1.0" encoding="utf-8"?>
<resources> <string name="app_name">Phone</string>
<string name="input_num">请输入号码</string>
<string name="action_settings">Settings</string>
<string name="call">呼叫此号码</string> </resources>

strings.xml:定义的一些变量、常量

package com.shellway.phone;

import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText; public class MainActivity extends ActionBarActivity implements OnClickListener { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//通过findViewById()方法获取按钮对象
Button button = (Button) findViewById(R.id.button1);
//给这个按钮添加一个监听对象
button.setOnClickListener(this);//因为本身实现了监听接口,所以用this } public void onClick(View v) {
EditText editText = (EditText) findViewById(R.id.et_phone);
String num = editText.getText().toString();
//创建一个意图
Intent intent = new Intent();
intent.setAction(Intent.ACTION_CALL);//给这个意图添加动作
intent.setData(Uri.parse("tel:" + num));//给这个意图添加数据
startActivity(intent);//新开一个窗体执行这个意图
}
}

MainActivity:主程序

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.shellway.phone"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.CALL_PHONE"/> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application> </manifest>

AndroidManifest.xml:请求权限清单

三.发送短信
1.步骤
在onClick方法中获取号码和内容
获取短信管理器
将内容分段
发送短信
弹出Toast提示
2.注意
短信和电话都属于付费功能, 需要声明权限: android.permission.SEND_SMS

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.shellway.sms.MainActivity" > <TextView
android:id="@+id/TextView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/number" />
<EditText
android:id="@+id/numberET"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="phone"
android:layout_below="@id/TextView1"
/>
<TextView
android:id="@+id/TextView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/content"
android:layout_below="@id/numberET"
/>
<EditText
android:id="@+id/contentET"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:lines="3"
android:layout_below="@id/TextView2"
/>
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/send"
android:layout_below="@id/contentET"
/> </RelativeLayout>

activity_main.xml:页面布局

<?xml version="1.0" encoding="utf-8"?>
<resources> <string name="app_name">SMS</string>
<string name="number">电话号码</string>
<string name="content">短信内容</string>
<string name="send">发送短信</string>
<string name="action_settings">Settings</string> </resources>

strings.xml:定义的一些变量、常量

package com.shellway.sms;

import java.util.ArrayList;

import android.support.v7.app.ActionBarActivity;
import android.telephony.SmsManager;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast; public class MainActivity extends ActionBarActivity implements OnClickListener { private EditText num;
private EditText content;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
num = (EditText) findViewById(R.id.numberET);
content = (EditText) findViewById(R.id.contentET);
Button button = (Button) findViewById(R.id.btn);
button.setOnClickListener(this);
} public void onClick(View v) {
String sms_number = num.getText().toString();
String sms_content = content.getText().toString();
SmsManager smsManager = SmsManager.getDefault();
// 将短信内容分割
ArrayList<String> list = smsManager.divideMessage(sms_content);
for (String s : list)
smsManager.sendTextMessage(sms_number, null, s, null, null);
// 获取上下文
Context context = getApplicationContext();
CharSequence text = "发送成功";// 通知内容
int duration = Toast.LENGTH_SHORT;// 弹出通知持续的时长
Toast toast = Toast.makeText(context, text, duration);
toast.show();
/*
//发送成功通知,这里等价于上面注释掉的5行内容
Toast.makeText(getApplicationContext(), "发送成功", Toast.LENGTH_SHORT).show();*/
}
}

MainActivity:主程序

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.shellway.sms"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.SEND_SMS"/> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application> </manifest>

AndroidManifest.xml:请求权限清单

四.布局
1.LinearLayout
分为水平和垂直两种, 可以嵌套使用

<?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="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/name"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/name"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/name"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/name"
/>
</LinearLayout> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/name"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/name"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/name"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/name"
/>
</LinearLayout>

LinearLayout

2.FrameLayout
后写的覆盖先写的

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:src="@drawable/transformers"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<ImageView
android:src="@drawable/play"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center"
/>
</FrameLayout>

FrameLayout

3.TableLayout
类似与HTML的<table>标签

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="1"
>
<!--1、 stretchColumns:指定元素伸缩,从0开始 。 gravity:指定元素的重心位置
2、若要平均分配宽度则用:layout_width="0dp"和layout_weight="1"组合。
它也可以用来占满剩下的位置,前面的1。1 1.2都为100dp 那么1.3可以用它们来占满剩下的位置
-->
<TableRow>
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:padding="10dp"
android:gravity="center"
android:text="1.1"
/>
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:padding="10dp"
android:gravity="center"
android:text="1.2"
/>
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:padding="10dp"
android:gravity="center"
android:text="1.3"
/>
</TableRow>
<TableRow>
<TextView
android:padding="10dp"
android:text="2.1"
/>
<TextView
android:padding="10dp"
android:gravity="center"
android:text="2.2"
/>
<TextView
android:padding="10dp"
android:text="2.3"
/>
</TableRow>
</TableLayout>

TableLayout

4.RelativeLayout
相对布局, 元素位置相对与其他元素定位

package com.shellway.layout;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View; public class MainActivity extends ActionBarActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.relativelayout);
} /**
* 因为在XML中给按钮设置了onClick属性,还没实现按钮监听并响应事件时,
* 为了在模拟器上点击按钮不让程序崩溃先给它加个个空的onClick方法
* @param view
*/
public void onClick(View view){ }
}

MainActivity

<?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"
android:padding="10dp" > <TextView
android:id="@+id/TextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:text="手机号码:"
/>
<EditText
android:id="@+id/EditText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/TextView1"
android:layout_marginLeft="25dp"
android:inputType="phone"
/>
<TextView
android:id="@+id/TextView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/EditText1"
android:textSize="15sp"
android:text="短信内容:"
/>
<EditText
android:id="@+id/EditText2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignTop="@id/TextView2"
android:layout_toRightOf="@id/TextView2"
android:layout_marginLeft="25dp"
android:inputType="textMultiLine"
android:lines="3"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/EditText2"
android:layout_alignParentRight="true"
android:onClick="onClick"
android:text="发送短信"
/> </RelativeLayout>

RelativeLayout

5.AbsoluteLayout
用坐标x,y定位元素的绝对位置

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" > <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="41dp"
android:layout_y="26dp"
android:text="Button" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="43dp"
android:layout_y="124dp"
android:text="Button" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="174dp"
android:layout_y="35dp"
android:text="Button" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="114dp"
android:layout_y="202dp"
android:text="Button" /> </AbsoluteLayout>

AbsoluteLayout

java攻城师之路(Android篇)--搭建开发环境、拨打电话、发送短信、布局例子的更多相关文章

  1. java攻城狮之路(Android篇)--widget_webview_metadata_popupwindow_tabhost_分页加载数据_菜单

    一.widget:桌面小控件1 写一个类extends AppWidgetProvider 2 在清单文件件中注册: <receiver android:name=".ExampleA ...

  2. java攻城狮之路(Android篇)--BroadcastReceiver&Service

    四大组件:activity 显示. contentProvider 对外暴露自己的数据给其他的应用程序.BroadcastReceiver 广播接收者,必须指定要接收的广播类型.必须明确的指定acti ...

  3. java攻城狮之路(Android篇)--MP3 MP4、拍照、国际化、样式主题、图片移动和缩放

    一.MP3播放器 查看Android API文档可以看到MediaPlayer状态转换图: 练习: package com.shellway.mp3player; import java.io.Fil ...

  4. java攻城狮之路(Android篇)--Activity生命

    一:Activity的激活 1.写一个类 extends Activity Activity是android的四大组件之一.Activity的激活分为显式意图激活和隐式意图激活.如果一个activit ...

  5. java攻城狮之路(Android篇)--ListView与ContentProvider

    一.ListView 1.三种Adapter构建ListView ListView添加条目的时候, 可以使用setAdapter(ListAdapter)方法, 常用的ListAdapter有三种 B ...

  6. java攻城狮之路(Android篇)--与服务器交互

    一.图片查看器和网页源码查看器 在输入地址的是不能输入127.0.0.1 或者是 localhost.ScrollView :可以看成一个滚轴 可以去包裹很多的控件在里面 练习1(图片查看器): pa ...

  7. java攻城狮之路(Android篇)--SQLite

    一.Junit    1.怎么使用        在AndroidManifest.xml文件中进行配置, 在manifest借点下配置instrumentation, 在application借点下 ...

  8. android 入门 002 (拨打电话,发送短信)

    一.拨打电话 1.首先做好界面,代码如下: layout =>activity_main.xml 中 <LinearLayout xmlns:android="http://sc ...

  9. java攻城师之路--复习java web之servlet

    需要掌握的知识点:1.Servlet程序编写 ----- 生命周期2.ServletAPI Request Response 3.Cookie 和 Session Servlet 用来 动态web资源 ...

随机推荐

  1. Nodejs学习笔记(九)--- 与Redis的交互(mranney/node_redis)入门

    目录 简介和安装 redis简介 redis安装 redis运行 node_redis安装 连接到redis服务器redis.createClient() 认证 client.auth(passwor ...

  2. 【Android】AppCompat V21:将 Materia Design 兼容到5.0之前的设备

    AppCompat V21:将 Materia Design 兼容到于5.0之前的设备 本篇文章翻译自Chris Banes(就职于Google,是Android-PullToRefresh,Phot ...

  3. js高仿QQ消息列表左滑功能

    该组件,主要功能类似于QQ消息列表左滑出现删除.标为已读等按钮的功能:现在的版本用的是纯javaScript编写:后续会跟进 angularJs 开发的类似组件以及jquery的; 下面,就让我们来认 ...

  4. This in JavaScript

    声明 本文仅为读书笔记并致力于理解Js中的this关键字.如有雷同,纯属巧合.原因有二,其一:有幸看了同一本书,其二:this这玩意已被说烂了~ 为什么要用this? this提供了优雅的方式隐式传递 ...

  5. 更改Windows系统的密码之后,SQL Server 2008服务无法启动

    问题:更改Windows操作系统的密码之后,SQL Server 2008服务无法启动. 原因:SQL Server服务需要使用操作系统的某个登录账户. 解决:需要在服务的属性窗口中修改账户密码,然后 ...

  6. 25数据查询的各种小玩法-select上(必学)-天轰穿sqlserver视频教程

    大纲:简单查询-选择数据列,使用字符串,改变列标题,使用数据运算 优酷超清地址,为了冲优酷的访问量,所以这里只放优酷的地址了,其实其他网站还是都传了的哈.

  7. 使用Gulp和Browserify来搭建React应用程序

    对React有一定了解之后,我们知道,需要把JSX文件转换成JS文件,组件需要导入导出.本篇就体验使用Gulp把JSX文件转换成JS文件,使用Browserify来把组件捆绑到一个文件并理顺组件之间的 ...

  8. spring4 security 4 +websocket 实现单点登录

    测试地址:http://sms.reyo.cn/ 帐号:aa 密码:123456 先看一下效果图: spring4 security 4 实现单点登录,而websocket 实现单点的下线通知

  9. Redis常用命令汇总

    Redis HGET获取与字段中存储的键哈希相关联的值D:\web\JH2016\RedisV3.2\2MasterOpenAPI-15698\redis-cli.exe -h 127.0.0.1 - ...

  10. easyui中 在子tabs中 添加新的tabs

    function addToParentTab(title, url) {            self.parent.addTabIgnoreExist(title, url, 'icon-cha ...