Android實戰教程第三篇之簡單實現撥打到電話功能_Android

來源:互聯網
上載者:User

本文執行個體為大家分享了Android打電話功能的實現代碼,需要一個文本輸入框輸入號碼,需要一個按鈕打電話。

本質:點擊按鈕,調用系統打電話功能。

xml布局檔案代碼::

<LinearLayout 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"  tools:context=".MainActivity"  android:orientation="vertical"  >   <TextView   android:layout_width="wrap_content"   android:layout_height="wrap_content"   android:text="請輸入號碼" />  <EditText   android:id="@+id/et_phone"   android:layout_width="match_parent"   android:layout_height="wrap_content"   />  <Button   android:id="@+id/bt_call"   android:layout_width="wrap_content"   android:layout_height="wrap_content"   android:text="撥打"   />  </LinearLayout> 

mainactivity中代碼:

package com.ydl.dialer;  import android.net.Uri; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText;  public class MainActivity extends Activity {   @Override  protected void onCreate(Bundle savedInstanceState) {   super.onCreate(savedInstanceState);   setContentView(R.layout.activity_main);      //給按鈕設定點擊偵聽   //1.拿到按鈕對象   Button bt = (Button) findViewById(R.id.bt_call);//Button類是View的子類,向下轉型要強轉。   //2.設定偵聽   bt.setOnClickListener(new MyListener());  }   class MyListener implements OnClickListener{    //按鈕被點擊時,此方法調用   @Override   public void onClick(View v) {    //擷取使用者輸入的號碼    EditText et = (EditText) findViewById(R.id.et_phone);    String phone = et.getText().toString();        //我們需要告訴系統,我們的動作:我要打電話    //建立意圖對象    Intent intent = new Intent();    //把打電話的動作ACTION_CALL封裝至意圖對象當中    intent.setAction(Intent.ACTION_CALL);    //設定打給誰    intent.setData(Uri.parse("tel:" + phone));//這個tel:必須要加上,表示我要打電話。否則不會有打電話功能,由於在打電話資訊清單檔裡設定了這個“協議”        //把動作告訴系統,啟動系統打電話功能。    startActivity(intent);   }     }   } 

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援雲棲社區。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.