1.首先建立一个android工程Phone 这里不再演示
PhoneActivity.java文件如下:
package jiao.jiao; 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 PhoneActivity extends Activity { /** Called when the activity is first created. */ private EditText editText; private Button button; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); editText= (EditText)this.findViewById(R.id.editText); button = (Button)this.findViewById(R.id.call); button.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { String phoneNum =editText.getText().toString(); Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+phoneNum)); PhoneActivity.this.startActivity(intent); } }); } } |
main.xml文件如下:
xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello" /> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/editText" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/call" android:text="@string/call_button" />
LinearLayout> |
strings.xml
xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">打电话string> <string name="app_name">Phonestring> <string name="call_button">打电话string> resources> |
最后要加一个打电话的权限
在工程中res->AndroidManifest.xml中
点击Add-----
点击Uses Permission-----
然后选择
这样就可以运行了!