用Android写了一个小程序,实现了打电话
2011-12-07 15:07
363 查看
今天我们用Android写了一个小程序,实现了打电话。首先,先新建一个Android项目Phone,然后修改Layout下Values中的String.xml,代码如下:
<?xmlversion="1.0" encoding="utf-8"?>
< resources>
<stringname="hello">Hello World, PhoneActivity!</string>
<string name="app_name">Phone</string>
<string name="input_into">请输入你要拨打的号码</string>
<string name="dial_caption">拨打</string>
</resources>
然后在main.xml中添加几个属性,代码如下:
<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中将自动生成如下代码:
publicstatic 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;
}
publicstatic 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文件中,有如下代码:
packagecn.class3g.activity;
importandroid.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;
publicclass 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) {
Stringphone_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-sdkandroid:minSdkVersion="10" />
<uses-permission android:name="android.permission.CALL_PHONE"/>
保存,模似打电话的程序就写完了,打开手机虚似机,右击项目名称,选Run As...—>AndroidApplication运行,便可拨打电话了。
注:该程序需启动两个手机虚似机,以实现互相拨号。
运行后效果如下图:
<?xmlversion="1.0" encoding="utf-8"?>
< resources>
<stringname="hello">Hello World, PhoneActivity!</string>
<string name="app_name">Phone</string>
<string name="input_into">请输入你要拨打的号码</string>
<string name="dial_caption">拨打</string>
</resources>
然后在main.xml中添加几个属性,代码如下:
<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中将自动生成如下代码:
publicstatic 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;
}
publicstatic 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文件中,有如下代码:
packagecn.class3g.activity;
importandroid.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;
publicclass 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) {
Stringphone_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-sdkandroid:minSdkVersion="10" />
<uses-permission android:name="android.permission.CALL_PHONE"/>
保存,模似打电话的程序就写完了,打开手机虚似机,右击项目名称,选Run As...—>AndroidApplication运行,便可拨打电话了。
注:该程序需启动两个手机虚似机,以实现互相拨号。
运行后效果如下图:
相关文章推荐
- Android模似打电话程序实现
- Android中实现“程序前后台切换效果”和“返回正在运行的程序,而不是一个新Activity”
- android 打电话程序的实现
- Android中实现“程序前后台切换效果”和“返回正在运行的程序,而不是一个新Activity”
- 微信小程序开发常用技巧(7)——实现一个类似于Android toast效果动画
- Android模拟打电话程序实现
- Android中实现“程序前后台切换效果”和“返回正在运行的程序,而不是一个新Activity”
- android多activity退出整个程序的一个实现
- 一个Android程序中新手引导功能实现方式的变迁
- Android中实现“程序前后台切换效果”和“返回正在运行的程序,而不是一个新Activity”
- android多activity退出整个程序的一个实现
- android多activity退出整个程序的一个实现
- 实现安卓程序退出后重进自己程序的一个小功能(android,unity)
- android多activity退出整个程序的一个实现
- Android中实现“程序前后台切换效果”和“返回正在运行的程序,而不是一个新Activity”
- Android简单的打电话程序的实现_学员原创
- Android模似打电话程序实现 - Android移动开发技术文章
- (转)android多activity退出整个程序的一个实现
- Android中实现“程序前后台切换效果”和“返回正在运行的程序,而不是一个新Activity”
- Delphi XE7 android 实现的在线更新APP的一个程序