Android菜鸟学习笔记

导航

 

一,界面:

电话拨号器界面

二,需求:输入电话号码,点击“拨号”按钮,调用系统自带拨号器拨打电话

三,知识点:

1,根据id查找View

2,为按钮添加点击事件

3,意图知识

四,代码:

MainActivity.java:

 1 package im.qvod.phone;
 2 
 3 import android.net.Uri;
 4 import android.os.Bundle;
 5 import android.app.Activity;
 6 import android.content.Intent;
 7 import android.view.View;
 8 import android.widget.Button;
 9 import android.widget.EditText;
10 
11 public class MainActivity extends Activity {
12     private EditText num;
13 
14     @Override
15     protected void onCreate(Bundle savedInstanceState) {
16         super.onCreate(savedInstanceState);
17         setContentView(R.layout.activity_main);
18         Button button = (Button) this.findViewById(R.id.button);
19         num = (EditText) findViewById(R.id.num);
20         button.setOnClickListener(new ButtonClickListener());
21     }
22 
23     private final class ButtonClickListener implements View.OnClickListener {
24         public void onClick(View v) {
25             String number = num.getText().toString();
26             Intent intent = new Intent();
27             intent.setAction("android.intent.action.CALL");
28             // intent.addCategory("android.intent.category.DEFAULT");
29             intent.setData(Uri.parse("tel:" + number));
30             startActivity(intent);// 方法内部会自动为Intent添加类别:android.intent.category.DEFAULT
31 
32         }
33 
34     }
35 //    public boolean onCreateOptionsMenu(Menu menu) {
36 //        // Inflate the menu; this adds items to the action bar if it is present.
37 //        getMenuInflater().inflate(R.menu.main, menu);
38 //        return true;
39 //    }
40 
41 }

 

activity_main.xml:

  

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

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

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/numEdit" 
        android:id="@+id/num"
        />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="@string/buttonText" />

</LinearLayout>

 

posted on 2013-04-03 14:47  Android菜鸟学习笔记  阅读(263)  评论(0)    收藏  举报