Android简单的打电话程序的实现
第一步:界面设计
第二步:整体实现:
打电话是手机的主要功能,我的Android第一个程序就是实现打电话功能。
1、首先在values目录下的strings.xml中添加相应的文字信息
<string name="input_info">请输入您要拨打的号码:</string> <string name="dial_caption">拨打</string>
2、然后在layout中实现页面的布局,在并且引入相应的文本信息
<!—实现页面标题的插入--> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/input_info" /> <!—定义文本框--> <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:id="@+id/dial_btn" android:text="@string/dial_caption" />
3、撰写java代码:
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 classPhoneActivity extends Activity { //定义文本框和按钮 EditText numberEt; Button dialBtn; publicvoidonCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); findViews(); dialBtn.setOnClickListener(new OnClickListener(){ public void onClick(View v) { //调用系统的拨号服务实现拨打功能 Stringphone_number = numberEt.getText().toString(); phone_number= phone_number.trim(); if(phone_number != null &&!phone_number.equals("")){ //封装一个拨打电话的intent,并且将电话号码包装成一个Uri对象传入 Intentintent = newIntent(Intent.ACTION_CALL,Uri.parse("tel:" + phone_number)); PhoneActivity.this.startActivity(intent); } } }); } public void findViews(){ numberEt = (EditText) this.findViewById(R.id.phone_number); dialBtn = (Button) this.findViewById(R.id.dial_btn); } }
4、不要忘了给它权限哦,在AndroidManifest.xml中写入
<uses-permission android:name="android.permission.CALL_PHONE"/>
5、启动两次模拟器,模拟5556给5554打电话.
在Android虚拟机中测试,测试图如下:
以上是简单的拨打电话的实现,希望你能够理解!