Home>

The following case simulates the implementation of an android phone dialer

androidmanifest.xml manifest list

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.ljq.phone"
  android:versioncode="1"
  android:versionname="1.0">
 <application android:icon="@ drawable/icon" 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-sdk android:minsdkversion="7" />
 <uses-permission android:name="android.permission.call_phone" />
</manifest>

main.xml layout file:

<?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="fill_parent"
  android:layout_height="wrap_content"
  android:text="Please enter a phone number" />
 <edittext android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:id="@ + id/phone" />
 <button android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="Dial this number"
  android:id="@ + id/button" />
</linearlayout>

mainactivity class:

package com.ljq.phone;
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 phone=null;
 @override
 public void oncreate (bundle savedinstancestate) {
  super.oncreate (savedinstancestate);
  setcontentview (r.layout.main);
  phone=(edittext) this.findviewbyid (r.id.phone);
  button button=(button) this.findviewbyid (r.id.button);
  button.setonclicklistener (new view.onclicklistener () {
   public void onclick (view v) {
    string tel=phone.gettext (). tostring ();
    //Method one, use intent purpose:Activate android component
    //intent intent=new intent ();
    //intent.setaction("android.intent.action.call ");
    //intent.setdata(uri.parse("tel:"+tel));
    //Method Two
    intent intent=new intent ("android.intent.action.call", uri.parse ("tel:" + tel));
    //The method's internal will automatically set the category for the intent object:
    startactivity (intent);
   }
  });
 }
}

operation result:

Interface initialization:

Phone call effect:

  • Previous iOS-UIButton (UIEdgeInsets)/Set the text and image on the button to be vertically centered
  • Next Simple code example for prime and greatest common divisor in Java