Android 撥號器

1、實現思路:java

一、實現界面,界面上有EditText  控件, Button 控件android

二、資源文件 values 下建立strings.xml 文件並定義使用到的string
app

三、values  下建立colors.xml 並定義使用的顏色相關信息ide

四、繼承Activity  類實現oncreate() 方法this

五、在activity中獲取獲取點的話好.net

六、爲這個button 控件添加監聽code

七、建立Intent 對象併爲這個對象設置活動和數據component

八、開始活動xml

2、使用到的主要類:對象

一、Intent  intent =  new Intent();               //信息傳遞的媒介 

3、設置Intent 的活動以及數據:

一、intent.setAction(Intent.ACTION_CALL);       //Intent.ACTION_CALL android系統中的常量活動爲撥號

二、intent.setData(Uri.parse("tel:"+獲取的電話號));

三、this.startActivity(intent);

***intent 由如下幾個部分組成:動做 action  數據 data    分類 Category   類型 type   組件component  以及擴展信息  Extra  .經過這些能夠啓動其餘組將而且攜帶信息。


3、實現代碼:


一、layout/main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center_horizontal"
     >
    <TextView
        android:id="@+id/textView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
        android:layout_marginTop="18px"
        />  
    <EditText
        android:id="@+id/editText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />
    <Button
        android:id="@+id/button"
        android:layout_width="250px"
        android:layout_height="wrap_content"
        android:text="撥打"           
        android:textColor="@color/text_color"
        android:background="@color/back_color"
        android:layout_marginTop="25px"
        />
</LinearLayout>


二、用到的strings:

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

    <string name="app_name">call</string>
    <string name="hello_world">電話撥號器</string>
    <string name="action_settings">select</string>

</resources>

三、colors:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color  name="text_color">#00CC00</color>    
<color name="back_color">#0000ff</color>
</resources>

四、Activity:

package com.inspur.call;

import android.support.v7.app.ActionBarActivity;
import android.text.Editable;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends ActionBarActivity {
    private EditText  editText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        // 放在這裏比放在方法中的效果比較好
         editText = (EditText)findViewById(R.id.editText);
        Button button=(Button)findViewById(R.id.button);
        // 獲取電話號,這裏進行了類型轉化
        
        //意圖建立
        
        //添加 按鈕觸發事件
        button.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                
                //  撥號是調用系統的撥號實現撥號功能
                
                String number= editText.getText().toString().trim();
               
               //系統會自動生成android.intent.category.DEFAULT
               Intent  intent= new Intent();
               intent.setAction(Intent.ACTION_CALL);
               intent.setData(Uri.parse("tel:"+number));
               MainActivity.this.startActivity(intent);
            
            };
            
        });
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

4、 清單文件中權限設置:

<uses-permission  android:name="android.permission.CALL_PHONE"/>

相關文章
相關標籤/搜索