8天學會android---電話撥號器

原理:用戶輸入電話號碼,當點擊撥打的時候,由監聽對象捕獲,監聽對象通過文本控件獲取到用戶輸入的電話號   碼,調用系統的撥號功能。

佈局界面:以線性佈局爲主,裏面有TextView,EditText,Button

實現代碼:Button的點擊事件,調用電話撥號器的意圖


具體代碼:

public class MainActivity extends Activity implements OnClickListener{

private Button btnSend;
private TextView textSend;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}

private void init() {
// TODO Auto-generated method stub
btnSend=(Button) this.findViewById(R.id.btnSend);
btnSend.setOnClickListener(this);
textSend=(EditText) this.findViewById(R.id.textSend);
}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btnSend:
String mobileStr=textSend.getText().toString().trim();
Intent intent=new Intent();
intent.setAction("android.intent.action.CALL");
// intent.addCategory("android.intent.category.DEFAULT");
intent.setData(Uri.parse("tel:"+mobileStr));
startActivity(intent);//方法內部會自動爲Intent添加類別,故可以省略
break;
}
}
}


佈局界面:

<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"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/mobile_name" />

    <EditText 
        android:id="@+id/textSend" 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="@string/mobile_name" />
    
    <Button
        android:id="@+id/btnSend" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/mobile_button" />/>
</LinearLayout>


下載地址:

http://download.csdn.net/detail/u010074054/8467993

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章