andriod 电话拨号器

第一个andriod小应用,对照着黑马视频14期电话拨号器写的,中间也遇到不少问题。
MainActivity.java
package com.stones.phone;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.os.Build;
import android.content.Intent;  
import android.net.Uri;  


public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        //取到按钮ID
        Button bt_dail=(Button)this.findViewById(R.id.bt_dail);  
        //定义按钮响应onClik()方法
        final  class MyClickLister implements View.OnClickListener{
            
            public void onClick(View v)
            {
               EditText et_number=(EditText)MainActivity.this.findViewById(R.id.et_number);
               String number=et_number.getText().toString(); 
               //创建一个做什么事情的意图   设置动作
               Intent intent=new Intent();
               intent.setAction(intent.ACTION_CALL);
               //uri类型的数据 统一资源标示符  设置动作对应的数据 注意电话格式tel后有冒号
               intent.setData(Uri.parse("tel:"+number));
               //开启新的界面,把动作放进去
               startActivity(intent);
            }
        }
        bt_dail.setOnClickListener(new MyClickLister()); 
        //MyClickLister cannot be resolved to a type 
        //原来是这里用MyClickLister()未定义,放在定义后面就没有错误了    
        //按钮事件需要设置一个接口类型的对象,需要实现接口

 

  Layout文件夹里的Activity_main.xml文件

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.stones.phone.MainActivity"
    tools:ignore="MergeRootFrame" >

    <EditText
        android:id="@+id/et_number"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="phone" >   
    </EditText>

    <Button
        android:id="@+id/bt_dail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/et_number"
        android:layout_alignParentRight="true"
        android:text="@string/dail" />

</RelativeLayout>

AndriodMainFest.xml文件添加拨打电话的权限

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />
    <uses-permission android:name="android.permission.CALL_PHONE"/>

 

posted on 2015-02-15 17:34  ++stone  阅读(103)  评论(0编辑  收藏  举报

导航