安卓笔记---对话框中输入,判断是否符合号码规范,是--跳转拨号盘,否--提示号码不正确

新建项目后,添加一个输入框和按钮

编辑activity_main.xml

<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.chris.android_4_4.MainActivity$PlaceholderFragment" >

    <Button
    //id改成TelBtn
        android:id="@+id/TelBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText1"
        android:layout_centerHorizontal="true"
        android:text="Button" />

    <EditText
    //id改成TelNum
        android:id="@+id/TelNum"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:ems="10" >

        <requestFocus />
    EditText>

RelativeLayout>

编辑MainActivity.java

package com.example.chris.telnum;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.telephony.PhoneNumberFormattingTextWatcher;
import android.telephony.PhoneNumberUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
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);
        //这里是编写的部分,调用TelBtn
        Button bCall = (Button)findViewById(R.id.TelBtn);
        //对按钮监听
        bCall.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        EditText telnum = (EditText)findViewById(R.id.TelNum);
                        String telNum = telnum.getText().toString();
                        //判断输入框中的内容是否符合号码规则,符合则跳转拨号界面并填充, 不符合提示号码不正确
                                              if(PhoneNumberUtils.isGlobalPhoneNumber(telNum)){
                            Intent i = new Intent(Intent.ACTION_DIAL, Uri.parse("tel://"+telNum));
                            MainActivity.this.startActivity(i);
                        }else{
                            Toast.makeText(MainActivity.this,"号码不正确",5000).show();
                        }
                    }
                }
        );
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

over!

你可能感兴趣的:(安卓备忘录)