您的位置:首页 > 移动开发 > Android开发

Android应用程序:简单拨号器

2015-08-11 17:47 513 查看
新建一个工程:Android Application Project,本人命名为tel。

MainActivity.java中的代码如下:

package com.example.tel;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {

private Button button;
private EditText call;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//去除标题栏
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
call = (EditText) findViewById(R.id.telphone);
button = (Button) findViewById(R.id.b);
button.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub
call = (EditText) findViewById(R.id.telphone);
String tel = call.getText().toString().trim();
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"+tel));
startActivity(intent);
}
});
}
}
activity_main.xml中的代码如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#E6E6E6" >
<!--
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#00ffff"
>
此处是设置的一个页面头部
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="拨号器"
android:textColor="@android:color/white"
android:textSize="18dp" />
</RelativeLayout>
-->
<ImageView
android:id="@+id/image_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="60dp"
android:src="@drawable/ic_launcher"/>

<EditText
android:id="@+id/telphone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="请输入手机号"
android:paddingLeft="10dp"
android:phoneNumber="true"
/>

<Button
android:id="@+id/b"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/login_button_nor"
android:text="拨打"
/>

</LinearLayout>
在AndroidManifest.xml中添加代码:
<uses-permission
android:name="android.permission.CALL_PHONE"></uses-permission>

补充说明:

             android:orientation="vertical" 表示竖直方式对齐。只有水平方向的设置才起作用,垂直方向的设置不起作用。即:left,right,center_horizontal 是生效的。

       android:orientation="horizontal"表示水平方式对齐。只有垂直方向的设置才起作用,水平方向的设置不起作用。即:top,bottom,center_vertical 是生效的。

       android:layout_width="fill_parent"定义当前视图在屏幕上可以消费的宽度,fill_parent即填充整个屏幕。

设置一个构件的布局为fill_parent将强制性地使构件扩展,以填充布局单元内尽可能多的空间。 

       android:layout_height="wrap_content":随着文字栏位的不同而改变这个视图的宽度或者高度。设置一个视图的尺寸为wrap_content将强制性地使视图扩展以显示全部内容。以TextView和ImageView控件为例,设置为wrap_content将完整显示其内部的文本和图像。布局元素将根据内容更改大小。
       layout_weight: 用于给一个线性布局中的诸多视图的重要度赋值.所有的视图都有一个layout_weight值,默认为零,意思是需要显示多大的视图就占据多大的屏幕空 间。若赋一个高于零的值,则将父视图中的可用空间分割,分割大小具体取决于每一个视图的layout_weight值以及该值在当前屏幕布局的整体layout_weight值和在其它视图屏幕布局的layout_weight值中所占的比率而定。
 

      举个例子:比如说我们在 水平方向上有一个文本标签和两个文本编辑元素。该文本标签并无指定layout_weight值,所以它将占据需要提供的最少空间。如果两个文本编辑元素每一个的layout_weight值都设置为1,则两者平分在父视图布局剩余的宽度(因为我们声明这两者的重要度相等)。如果两个文本编辑元素其中第一个的layout_weight值设置为1,而第二个的设置为2,则剩余空间的三分之一分给第一个,三分之二分给第二个。

      android:phoneNumber="true" :当前组件只能输入数字
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android 应用 打电话