android_01_电话拨号器

来源:互联网 发布:北交大知行pt网址 编辑:程序博客网 时间:2024/06/05 10:53

界面:


布局(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:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    android:orientation="vertical"    tools:context="com.xsism.helloworld.MainActivity" >    <TextView        android:id="@+id/textView2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/hello_world" />     <TextView        android:id="@+id/textView3"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignLeft="@+id/textView2"        android:layout_below="@+id/textView1"        android:layout_marginTop="33dp"        android:text="请输入号码:" />        <EditText        android:id="@+id/editText_phoneNo"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="10086"        /><Button    android:id="@+id/dialBtn"                android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignLeft="@+id/textView3"        android:layout_below="@+id/textView3"        android:layout_marginTop="21dp"        android:text="拨号" /></LinearLayout>





权限(AndroidManifest.xml):

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.xsism.helloworld"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="21" />    <uses-feature />    <package-verifier />    <uses-permission android:name="android.permission.CALL_PHONE"/>    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name=".MainActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest>




package com.xsism.helloworld;import android.support.v7.app.ActionBarActivity;import android.content.Intent;import android.net.Uri;import android.os.Bundle;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.*;import android.view.MenuItem;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;public class MainActivity extends ActionBarActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        // 设置布局文件        setContentView(R.layout.activity_main);        // 获取按钮        Button btn = (Button)findViewById(R.id.dialBtn);        // 设置侦听        btn.setOnClickListener(new BtnListener());    }        // 内部类    class BtnListener implements OnClickListener{@Overridepublic void onClick(View v) {// TODO Auto-generated method stub//System.out.println("sg__btn clicked");//Toast.makeText(getApplicationContext(), "默认Toast样式",//     Toast.LENGTH_SHORT).show();// 获取输入框EditText phoneNoEdit = (EditText) findViewById(R.id.editText_phoneNo);String phoneNoStr = phoneNoEdit.getText().toString();Toast.makeText(getApplicationContext(), phoneNoStr,     Toast.LENGTH_SHORT).show();// 配置IntentIntent intent = new Intent();intent.setAction(Intent.ACTION_CALL);intent.setData(Uri.parse("tel:"+phoneNoStr));// 启动系统的call_phone (此处应该有权限)startActivity(intent);}        }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.main, menu);        return true;    }    @Override    public boolean onOptionsItemSelected(MenuItem item) {        // Handle action bar item clicks here. The action bar will        // automatically handle clicks on the Home/Up button, so long        // as you specify a parent activity in AndroidManifest.xml.        int id = item.getItemId();        if (id == R.id.action_settings) {            return true;        }        return super.onOptionsItemSelected(item);    }}

















0 0
原创粉丝点击