本文主要是介绍蜗牛—Android基础之拨打电话,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Java代码
package com.wjj.phone;import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;public class MainActivity extends Activity {private Button button;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);button = (Button) findViewById(R.id.button);ButtonClickListener buttonClickListener = new ButtonClickListener();button.setOnClickListener(buttonClickListener);}private class ButtonClickListener implements OnClickListener {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubEditText editText = (EditText) findViewById(R.id.mobile);String number = editText.getText().toString();Intent intent = new Intent();intent.setAction("android.intent.action.CALL");// intent.addCategory("android.intent.category.DEFAULT");intent.setData(Uri.parse("tel:" + number));startActivity(intent); // 方法内部会自动添加类别intent.addCategory("android.intent.category.DEFAULT");}}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}
XML--布局
<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=".MainActivity" ><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/mobile" /><EditTextandroid:id="@+id/mobile"android:layout_width="fill_parent"android:layout_height="wrap_content" /><Buttonandroid:id="@+id/button"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/boda" /></RelativeLayout>
XML--main
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.wjj.phone"android:versionCode="1"android:versionName="1.0" ><uses-sdkandroid:minSdkVersion="8"android:targetSdkVersion="18" /><!-- 拨打电话的权限 --><uses-permission android:name="android.permission.CALL_PHONE" /><applicationandroid:allowBackup="true"android:icon="@drawable/ic_launcher"android:label="@string/app_name"android:theme="@style/AppTheme" ><activityandroid:name="com.wjj.phone.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>
这篇关于蜗牛—Android基础之拨打电话的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!