[android] 電話撥號器

來源:互聯網
上載者:User

標籤:

1. activity代碼

 

package com.tsh.dail;import android.app.Activity;import android.content.Intent;import android.net.Uri;import android.os.Bundle;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);        //找到按鈕        //Button 繼承自TextView 繼承自View,父類型強制轉換成子類型        Button bt_dail=(Button) findViewById(R.id.bt_dail);        //給按鈕設定點擊事件,參數是OnClickListener的interface介面類型        bt_dail.setOnClickListener(new MyListener());    }    /**     * 注意OnClickListener介面不要導錯了,View下的     * @author taoshihan     *     */    private class MyListener implements OnClickListener{        /**         * 當按鈕點擊時調用這個方法         */        @Override        public void onClick(View v) {            // TODO Auto-generated method stub            //找到輸入框,內部類調用外部類的方法            //EditText繼承自TextView,繼承自View            EditText et_number=(EditText) findViewById(R.id.et_number);            //物件導向的思想            String number=et_number.getText().toString();            //組件之間松耦合的思想,意圖做啥,意圖打電話,電話號碼是啥            Intent intent=new Intent();            intent.setAction(Intent.ACTION_CALL);            //Uri類型 統一資源識別項,Url 統一資源定位器 Uri是Url他爹            intent.setData(Uri.parse("tel:"+number));            //開啟一個介面            startActivity(intent);            //別忘了開啟許可權喲        }            }}

 

2. layout代碼

<RelativeLayout 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"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.tsh.dail.MainActivity" >    <!-- [android:id="@+id/my_number" R檔案中增加一個id] -->   <EditText        android:id="@+id/et_number"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:inputType="phone" >    </EditText>    <!-- [直接抽取字串ctrl+1] -->    <!-- [遇到黃色歎號消不掉,clear一下工程] -->    <!-- [layout_alignParentRight:與父表單的右邊對齊] -->    <!-- [android:layout_below="@id/my_number":位於此id控制項的下面,@idR檔案這的這個id] -->       <!-- [id的命名,button bt_xxx,edittext et_xxx] -->   <Button       android:id="@+id/bt_dail"       android:layout_width="wrap_content"       android:layout_height="wrap_content"       android:layout_alignParentRight="true"          android:layout_below="@id/et_number"       android:text="@string/dail" /></RelativeLayout>

 

[android] 電話撥號器

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.