Android模似打电话程序实现
今天我们用Android写了一个小程序,实现了打电话。首先,先新建一个Android项目Phone,然后修改Layout下Values中的String.xml,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, PhoneActivity!</string>
<string name="app_name">Phone</string>
<string name="input_into">请输入你要拨打的号码</string>
<string name="dial_caption">拨打</string>
</resources>
然后在main.xml中添加几个属性,代码如下:www.zzzyk.com
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/input_into" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/phone_number"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/dial_caption"
android:id="@+id/dial_btn"/>t
layout为布局,修改Main.xml的代码后将有如图的显示效果:
在R.Java中将自动生成如下代码:
public static final class drawable {
public static final int ic_launcher=0x7f020000;
}
public static final class id {
public static final int dial_btn=0x7f050001;
public static final int phone_number=0x7f050000;
}
public static final class string {
public static final int app_name=0x7f040001;
public static final int dial_caption=0x7f040003;
public static final int hello=0x7f040000;
public static final int input_into=0x7f040002;
}
在PhoneActivity.java文件中,有如下代码:
package cn.class3g.activity;
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;
public class PhoneActivity extends Activity {
EditText numberEt;
Button dialBtn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViews();
dialBtn.setOnClickListener(myListener);
}
public void findViews(){
numberEt = (EditText)this.findViewById(R.id.phone_number);
dialBtn =(Button) this.findViewById(R.id.dial_btn);
}
private OnClickListener myListener = new Button.OnClickListener(){
public void onClick(View v) {
String phone_number = numberEt.getText().toString();
phone_number = phone_number.trim();
if(phone_number != null && !phone_number.equals("")){
Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+phone_number));
PhoneActivity.this.startActivity(intent);
}
}
};
}
在AndroidManifest.xml中<application上方添加:
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.CALL_PHONE"/>
保存,模似打电话的程序就写完了,打开手机虚似机,右击项目名称,选Run As...—>Android Application运行,便可拨打电话了。
注:该程序需启动两个手机虚似机,以实现互相拨号。
运行后效果如下图:
摘自 LLLAZY520的专栏
补充:移动开发 , Android ,