Android电话拨号器案例

Android电话拨号器案例:


项目最终样式:





首先 我们我在eclipse中创建一个AndroidApplicationProject项目 项目创建完毕后,我们需要对我们的项目进行布局.

对于布局来说, 我们这可以采用线性布局(LinearLayout)  然后我们需要注意的一点就是我们布局的方向问题 也就是xml文件中orientation中的属性应该是垂直排列的(vertical)

代码如下:

 OK!

接下来我们就是插入的我们的控件了!上面的样式中我们看到基本的控件有三个.

(1)TextView

(2)EditText

(3)Button

因此我们的布局文件代码如下:

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

<!--
我们应该养成一个好习惯 我的叫法是"归类" 比如说我们text属性值我们应该将该值存放到对应的
目录下面@代表的是引用该文件中的数据  在我们的res 下 valuse 下 string.xml
中,这里面存放的是字符的信息
 -->
    <TextView 
        android:id="@+id/main_tv_tag"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/main_tv"
        />
    <EditText 
        android:id="@+id/main_et_phonenumber"
<span style="white-space:pre">	</span>android:inputType="number"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
    <Button 
        android:id="@+id/callphone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/main_but_call"
        />

</LinearLayout>
</pre><pre name="code" class="html">
</pre><pre name="code" class="html">
</pre><pre name="code" class="html"><span style="font-size:24px;"><strong>布局完成了!是不是很简单呢!(*</strong></span>
<span style="font-size:24px;"><strong>那么我接下就该在我们的代码中去实现拨打电话的功能了</strong></span>
<span style="font-size:24px;"><strong>Let's Go!</strong></span>

package com.example.callphone;

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

public class MainActivity extends Activity {
	// 将我们的控件定义成全局变量 方便使用
	private EditText phoneNumber;
	private Button callPhone;
	

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        // 我们要初始化控件
        
        phoneNumber = (EditText) findViewById(R.id.main_et_phonenumber);
        callPhone = (Button) findViewById(R.id.callphone);
        
        
        // 首先我们应该明确一点 当我们输入我们的需要拨打的手机号时 输入完成之后 点击按钮就开始执行拨打的功能了
        // 因此我们需要对该按钮进行监听 就是说这个按钮有没有单击过
        
        // 这里是我们使用的是内部匿名对象 其实对于按钮的单击事件 我们可以有四种方法 这个我们以后细说
        callPhone.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// 获取输入框中内容
				String number = phoneNumber.getText().toString();
				// 判断一下 万一用户输入的是空值呢
				if(TextUtils.isEmpty(number)) {
					// 给个提示
					Toast.makeText(MainActivity.this, "手机号不能为空!", Toast.LENGTH_LONG).show();
					return; // 如果输入为空我们就不向下执行了
				} else {
					// 那就开始拨打电话呗
					Intent intent = new Intent(); // 意图
					intent.setAction(intent.ACTION_CALL);
					intent.setData(Uri.parse("tel:" + number));
					startActivity(intent);
					
				}
			}
		});
        
    }


  
    
}


别忘记打电话的权限:  android.permission.CALL_PHONE


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值