首先頁面部分就不在這裏說了,界面如圖:
1,核心代碼部分
package com.example.ytbksofttest;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.content.Intent;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*代碼開始部分*/
Button button=(Button)findViewById(R.id.button1);//獲取button按鈕
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText phoneonText=(EditText)findViewById(R.id.editText1);
String phoneno=phoneonText.getText().toString();//獲取手機號碼
if ((phoneno!=null) && (!"".equals(phoneno.trim())))
{
Intent intent=new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+phoneno));//創建一個撥號意圖
startActivity(intent);//發送意圖
}
}
});
/*代碼結束*/
}
@Override
public 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;
}
}
2,以上代碼完成之後,還需要在AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ytbksofttest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/katong2"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.ytbksofttest.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>
<!-- 這裏就是申請權限 -->
<uses-permission android:name="android.permission.CALL_PHONE"/>
</manifest>