android(3)_撥打到電話操作

來源:互聯網
上載者:User

 

 

 

 


撥打到電話項目:

 

 

 

 

 

 

 

 

 

 

 


activity_phone.xml


[html] 
<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=".PhoneActivity" > 
 
    <!-- 
    android:id:id 
    布局:wrap_content包裹 
    android:layout_width:寬 
    android:layout_height:高 
    text: 顯示的文本引用 
    android:layout_below:在什麼下方 
    --> 
 
    <TextView 
        android:id="@+id/viewphone" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="@string/phone" /> 
 
    <EditText 
        android:id="@+id/editphone" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignLeft="@+id/viewphone" 
        android:layout_below="@+id/viewphone" 
        android:layout_marginTop="16dp" 
        android:ems="10" 
        android:inputType="phone" > 
 
        <requestFocus /> 
    </EditText> 
 
    <Button 
        android:id="@+id/btnphone" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignLeft="@+id/editphone" 
        android:layout_below="@+id/editphone" 
        android:layout_marginTop="23dp" 
        android:text="@string/buttonphone" /> 
 
</RelativeLayout> 

<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=".PhoneActivity" >

    <!--
    android:id:id
 布局:wrap_content包裹
 android:layout_width:寬
 android:layout_height:高
 text: 顯示的文本引用
 android:layout_below:在什麼下方
    -->

    <TextView
        android:id="@+id/viewphone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/phone" />

    <EditText
        android:id="@+id/editphone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/viewphone"
        android:layout_below="@+id/viewphone"
        android:layout_marginTop="16dp"
        android:ems="10"
        android:inputType="phone" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/btnphone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editphone"
        android:layout_below="@+id/editphone"
        android:layout_marginTop="23dp"
        android:text="@string/buttonphone" />

</RelativeLayout>
strings.xml


[html] 
<?xml version="1.0" encoding="utf-8"?> 
<resources> 
 
    <string name="app_name">lession01_phone</string> 
    <string name="action_settings">Settings</string> 
    <string name="phone">撥打到電話</string> 
    <string name="buttonphone">撥打</string> 
 
</resources> 

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">lession01_phone</string>
    <string name="action_settings">Settings</string>
    <string name="phone">撥打到電話</string>
    <string name="buttonphone">撥打</string>

</resources>

添加許可權

 

 

 

PhoneActivity.java

 

 

[java]
package com.example.lession01_phone; 
 
import android.net.Uri; 
import android.os.Bundle; 
import android.app.Activity; 
import android.content.Intent; 
import android.view.Menu; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 
 
public class PhoneActivity extends Activity { 
 
    private Button button_phone; 
    private EditText edit_phone; 
 
 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_phone); 
         
        button_phone = (Button) findViewById(R.id.btnphone); 
        edit_phone = (EditText)findViewById(R.id.editphone); 
         
        button_phone.setOnClickListener(new View.OnClickListener() { 
             
            @Override 
            public void onClick(View v) { 
                String phone_Num = edit_phone.getText().toString(); 
                 
                Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+phone_Num)); 
                 
                PhoneActivity.this.startActivity(intent); 
                 
                Toast.makeText(PhoneActivity.this, "正在給"+phone_Num+"撥打",Toast.LENGTH_LONG).show(); 
            } 
        }); 
         
    } 
 
 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
        // Inflate the menu; this adds items to the action bar if it is present.  
        getMenuInflater().inflate(R.menu.phone, menu); 
        return true; 
    } 
     

package com.example.lession01_phone;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class PhoneActivity extends Activity {

    private Button button_phone;
 private EditText edit_phone;


 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_phone);
       
        button_phone = (Button) findViewById(R.id.btnphone);
        edit_phone = (EditText)findViewById(R.id.editphone);
       
        button_phone.setOnClickListener(new View.OnClickListener() {
   
   @Override
   public void onClick(View v) {
    String phone_Num = edit_phone.getText().toString();
    
    Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+phone_Num));
    
    PhoneActivity.this.startActivity(intent);
    
    Toast.makeText(PhoneActivity.this, "正在給"+phone_Num+"撥打",Toast.LENGTH_LONG).show();
   }
  });
       
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.phone, menu);
        return true;
    }
   
}

運行效果:

 

 

 

 


 

相關文章

聯繫我們

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