一.搭建开发环境
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. 【Android】Android开源项目分类汇总

    第一部分 个性化控件(View) 主要介绍那些不错个性化的View,包括ListView.ActionBar.Menu.ViewPager.Gallery.GridView.ImageView.Pro ...

  2. 【系统移植】Android系统移植

    $ . .. Device     . SimulatorWhich would you like] Build type choices are. release     . debugWhich ...

  3. crossplatform---bower解决js的依赖管理

    从零开始nodejs系列文章,将介绍如何利Javascript做为服务端脚本,通过Nodejs框架web开发.Nodejs框架是基于V8的引擎,是目前速度最快的Javascript引擎.chrome浏 ...

  4. atitit. groupby linq的实现(1)-----linq框架选型 java .net php

    atitit.  groupby linq的实现(1)-----linq框架选型 java .net php 实现方式有如下 1. Dsl/ Java8 Streams AP ,对象化的查询api , ...

  5. Cento OS 6.5 YUM 安装 R 的方法

    (1)配置yum (2)安装EPEL YUM源 yum install  epel-release 修改源配置文件/etc/yum.repos.d/epel.repo ,把基础的恢复,镜像的地址注释掉 ...

  6. IT 需要知道的一些专业名词和解释 (长期更新)

    c2c: 就是我卖东西你来买  (comsumer to comsumer个人对个人) b2c: 就是我开公司卖东西你来买 (business to comsumer 公司对个人) o2o: 就是我开 ...

  7. jQuery实现Checkbox中项目开发全选全不选的使用

    <html> <head> <meta charset="utf-8"> <title>Checkbox的练习</title& ...

  8. Leetcode 342 Power of Four 数论

    题意:判断一个数是不是4的幂数,和Power of two类似. 先判断num是否大于0,再判断num是否能开根号,最后判断num开根号后的数是否是2^15的约数. 提示:4的幂数开根号就是2的幂数. ...

  9. SAFS Init Files

    There're many deployment files for configuration. We need to learn how SAFS read these depolyment fi ...

  10. Ubuntu下无法安装sun-java6-jdk的解决办法

    http://blog.sina.com.cn/s/blog_6296abc601018p86.html 这个帖子是讲怎么添加一个新的源, deb http://us.archive.ubuntu.c ...