第一个android程序 拨号器
1.main.xml-----布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/title" />
<EditText
android:id="@+id/number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:layout_below="@id/title" />
<Button
android:id="@+id/btn_call"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/number"
android:text="@string/callNumber" />
</RelativeLayout>
2.strings.xml-----资源文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">拨号器</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="title">请输入手机号</string>
<string name="callNumber">拨打此号码</string>
</resources>
3.AndroidManifest.xml-----配置文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.itheima28.caller"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="17" />
<!-- 添加拨打电话的权限 -->
<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="com.itheima28.caller.MainUI"
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>
4.Activity程序----MainUI.java(点击事件方式1)
package com.itheima28.caller;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
/**
* @author andong
* 程序刚运行就显示的界面
*/
public class MainUI extends Activity {
/**
* 当界面刚被创建时回调此方法
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); // 必须执行此句代码. 执行父类的初始化操作.
setContentView(R.layout.main); <span style="white-space:pre"> </span>// 设置当前界面显示的布局.
}
/**
* 当拨打此号码的按钮被点击时触发此方法.
* @param v
*/
public void call(View v) {
System.out.println("拨打电话.");
// 1. 取出输入框中的号码
EditText etNumber = (EditText) findViewById(R.id.number); // 输入框对象
String number = etNumber.getText().toString(); <span style="white-space:pre"> </span>// 将要拨打的号码
// 2. 根据号码拨打电话
Intent intent = new Intent(); <span style="white-space:pre"> </span>// 创建一个意图
intent.setAction(Intent.ACTION_CALL); // 指定其动作为拨打电话
intent.setData(Uri.parse("tel:" + number)); // 指定将要拨出的号码
startActivity(intent); <span style="white-space:pre"> </span>// 执行这个动作
}
}
5.Activity程序----MainUI2.java(点击事件方式2)
package com.itheima28.caller;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
/**
* @author andong
* 程序刚运行就显示的界面
*/
public class MainUI2 extends Activity {
/**
* 当界面刚被创建时回调此方法
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); <span style="white-space:pre"> </span>// 必须执行此句代码. 执行父类的初始化操作.
setContentView(R.layout.main); <span style="white-space:pre"> </span>// 设置当前界面显示的布局.
Button btnCall = (Button) findViewById(R.id.btn_call);
btnCall.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("MainUI2 拨打电话.");
call();
}
});
}
/**
* 拨打电话的业务方法
*/
private void call() {
// 1. 取出输入框中的号码
EditText etNumber = (EditText) findViewById(R.id.number); // 输入框对象
String number = etNumber.getText().toString(); <span style="white-space:pre"> </span>// 将要拨打的号码
// 2. 根据号码拨打电话
Intent intent = new Intent(); <span style="white-space:pre"> </span>// 创建一个意图
intent.setAction(Intent.ACTION_CALL); <span style="white-space:pre"> </span>// 指定其动作为拨打电话
intent.setData(Uri.parse("tel:" + number)); <span style="white-space:pre"> </span>// 指定将要拨出的号码
startActivity(intent); <span style="white-space:pre"> </span>// 执行这个动作
}
}
6.Activity程序----MainUI3.java(点击事件方式3)
package com.itheima28.caller;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
/**
* @author andong
* 程序刚运行就显示的界面
*/
public class MainUI3 extends Activity {
/**
* 当界面刚被创建时回调此方法
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); // 必须执行此句代码. 执行父类的初始化操作.
setContentView(R.layout.main); <span style="white-space:pre"> </span>// 设置当前界面显示的布局.
Button btnCall = (Button) findViewById(R.id.btn_call);
btnCall.setOnClickListener(new MyOnClickListener());
}
class MyOnClickListener implements OnClickListener {
@Override
public void onClick(View v) {
System.out.println("MainUI3 拨打号码..");
call();
}
}
/**
* 拨打电话的业务方法
*/
private void call() {
// 1. 取出输入框中的号码
EditText etNumber = (EditText) findViewById(R.id.number); // 输入框对象
String number = etNumber.getText().toString(); <span style="white-space:pre"> </span>// 将要拨打的号码
// 2. 根据号码拨打电话
Intent intent = new Intent(); <span style="white-space:pre"> </span>// 创建一个意图
intent.setAction(Intent.ACTION_CALL); <span style="white-space:pre"> </span>// 指定其动作为拨打电话
intent.setData(Uri.parse("tel:" + number)); <span style="white-space:pre"> </span>// 指定将要拨出的号码
startActivity(intent); <span style="white-space:pre"> </span>// 执行这个动作
}
}
7.Activity程序----MainUI4.java(点击事件方式4)
package com.itheima28.caller;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
/**
* @author andong
* 程序刚运行就显示的界面
*/
public class MainUI4 extends Activity implements OnClickListener {
/**
* 当界面刚被创建时回调此方法
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); // 必须执行此句代码. 执行父类的初始化操作.
setContentView(R.layout.main); <span style="white-space:pre"> </span>// 设置当前界面显示的布局.
Button btnCall = (Button) findViewById(R.id.btn_call);
btnCall.setOnClickListener(this);
}
/**
* 拨打电话的业务方法
*/
private void call() {
// 1. 取出输入框中的号码
EditText etNumber = (EditText) findViewById(R.id.number); // 输入框对象
String number = etNumber.getText().toString(); <span style="white-space:pre"> </span>// 将要拨打的号码
// 2. 根据号码拨打电话
Intent intent = new Intent(); <span style="white-space:pre"> </span>// 创建一个意图
intent.setAction(Intent.ACTION_CALL); <span style="white-space:pre"> </span>// 指定其动作为拨打电话
intent.setData(Uri.parse("tel:" + number)); <span style="white-space:pre"> </span>// 指定将要拨出的号码
startActivity(intent); <span style="white-space:pre"> </span>// 执行这个动作
}
@Override
public void onClick(View v) {
System.out.println("MainUI4 拨打号码..");
call();
}
}