电话拨号器

activity_main.xml 文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.mahaining.test1.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="拨打电话"
android:id="@+id/but1"
android:background="#8e75d1"
android:layout_below="@+id/ed1"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="phone"
android:ems="10"
android:id="@+id/ed1"
android:layout_marginTop="50dp"
android:hint="@string/Texthin"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="按钮"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="89dp"
android:id="@+id/but2"
/>

</RelativeLayout>

java代码

package com.example.mahaining.test1;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
private Button but1,but2;
private EditText ed1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//将布局文件引入activity 中
setContentView(R.layout.activity_main);
ed1=(EditText) findViewById(R.id.ed1);
but1=(Button) findViewById(R.id.but1);
but2=(Button) findViewById(R.id.but2);
//点击事件
but2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this,"点击率",Toast.LENGTH_LONG).show();
}
});

but1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//拿到编辑框号码
String inputStr = ed1.getText().toString();
//如果输入不为空创建打电话的Intent
if(inputStr.trim().length()!=0)
{// 创建意图对象
Intent intent = new Intent();
//直接拨打ACTION_CALL
// Intent intentPhone = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phoneNumber));
// startActivity(intentPhone);
// 2)跳转到拨号界面
// Intent intent = newIntent(Intent.ACTION_DIAL,Uri.parse("tel:" + phoneNumber));
// intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// startActivity(intent);

// 2、跳转到联系人页面,使用一下代码:
// Intent intentPhone = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phoneNumber));
// startActivity(intentPhone);
// 设定打电话这个动作
intent.setAction(Intent.ACTION_CALL);
// 设定获取电话号码
intent.setData(Uri.parse("tel:"+inputStr));

startActivity(intent);
}
//否则Toast提示一下
else{
Toast.makeText(MainActivity.this, "不能输入为空", Toast.LENGTH_LONG).show();
}
}

});
}
}








































































































posted @ 2016-07-12 14:28  mahaining  阅读(187)  评论(0编辑  收藏  举报