Android实现拨号功能

拨号界面:
   
   
   
   
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <!-- match_parent: 匹配父元素,父元素有多宽,我就有多宽;父元素有多高,我就有多高 --> <!-- wrap_content:包裹内容,个人内容有多高,我就有多高,个人内容有多宽,我就有多宽 --> <EditText android:id="@+id/et_input_num" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="phone" > </EditText> <!-- android:text: 指定按钮上的显示文字 --> <!-- android:onClick: 定义点击事件 --> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="call" android:text="打电话" /></LinearLayout>

// v: 表明你点击的控件
public void call(View view) {       
    EditText editText = (EditText) findViewById(R.id.input_num);        
    // 获取用户输入的手机号        
    String phone_number = editText.getText().toString();
    //删除字符串首部和尾部的空格   
        phone_number = phone_number.trim();     
    /**         
    * context : 上下文,环境 <br/>         
    * text : 要显示的内容 <br/>        
    * duration : 显示的时常   //持续时间         
             
    */        
    Toast.makeText(thisphone_number, Toast.LENGTH_LONG);
        if(phone_number != null && !phone_number.equals("")) {  
        //调用系统的拨号服务实现电话拨打功能 
        
    // 打电话        
    // Intent : 意图.我想去作一件事        
    Intent t = new Intent();        
    // Action:动做.我具体想作什么事        
    // Intent.ACTION_DIAL: 激活拨号界面        
    // Intent.ACTION_CALL: 直接拨打电话       
    t.setAction(Intent.ACTION_CALL);        
    // Data: 数据.具体的动做所须要的附加数据   
        //封装一个拨打电话的intent,而且将电话号码包装成一个Uri对象传     
    t.setData(Uri.parse("tel:" + phone_number));        
    // 通知系统你去帮我干活吧        
    startActivity(t);}          
}
最后在在AndroidManifest.xml里面添加
<uses-permission android:name="android.permission.CALL_PHONE"/> 


备注: Toast. makeText()使用方
1.默认的显示方式

2.自定义显示位置
3.带图片的
4.彻底自定义显示方式





相关文章
相关标签/搜索