核心代码四部分: 1、Layout 2、String值 3、MainActivity 4、给项目添加使用授权 ?xml version="1.0" encoding="utf-8"? LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_paren
核心代码四部分:
1、Layout
2、String值
3、MainActivity
4、给项目添加使用授权
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/mobile" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/mobile"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button"
android:id="@+id/button"
/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, FirstPhoneActivity!</string>
<string name="app_name">电话拨号器</string>
<string name="mobile">请输入手机号</string>
<string name="button">拨号</string>
</resources>
package com.dcj;
import com.dcj.R;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
private EditText mobileText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mobileText = (EditText)findViewById(R.id.mobile);
Button button = (Button)this.findViewById(R.id.button);
button.setOnClickListener(new ButtonClickListener());
}
private final class ButtonClickListener implements View.OnClickListener {
public void onClick(View v) {
String number = mobileText.getText().toString();
//Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+number) ); 等同于以下三行代码
Intent intent = new Intent();
intent.setAction("android.intent.action.CALL");
intent.setData(Uri.parse("tel:"+number));
startActivity(intent); //方法内部会自动为Intent添加类别:android.intent.category.DEFAULT
}
}
}
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.dcj"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".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>