安卓案例1:拨号器

package org.dreamtech.helloworld;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
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 et_number;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et_number = (EditText) findViewById(R.id.editText1);
        Button btn_call = (Button) findViewById(R.id.button1);
        btn_call.setOnClickListener(new MyClickListener());
    }
    
    private class MyClickListener implements OnClickListener{

        @Override
        public void onClick(View v) {
            //get
            String number = et_number.getText().toString().trim();
            if("".equals(number)){
                Toast.makeText(MainActivity.this, "不能为空", Toast.LENGTH_LONG).show();
                return;
            }
            
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_CALL);
            intent.setData(Uri.parse("tel:"+number));
            startActivity(intent);
        }
        
    }
}

注意设置权限:

安卓案例1:拨号器

布局:

安卓案例1:拨号器

上边采用的是按钮点击事件第一种:内部类

第二种:匿名内部类

btn_call.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                
            }
        });

第三种:实现接口(多按钮时候推荐使用)

package org.dreamtech.helloworld;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
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 implements OnClickListener {

    private EditText et_number;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et_number = (EditText) findViewById(R.id.editText1);
        Button btn_call = (Button) findViewById(R.id.button1);
        
        btn_call.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        String number = et_number.getText().toString().trim();
        if("".equals(number)){
            Toast.makeText(MainActivity.this, "不能为空", Toast.LENGTH_LONG).show();
            return;
        }
        
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_CALL);
        intent.setData(Uri.parse("tel:"+number));
        startActivity(intent);
        
    }
}

第四种:声明方法

<Button
        android:onClick="click"
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/editText1"
        android:text="拨打" />
package org.dreamtech.helloworld;

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

public class MainActivity extends Activity {

    private EditText et_number;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et_number = (EditText) findViewById(R.id.editText1);
        Button btn_call = (Button) findViewById(R.id.button1);
    }
    
    public void click(View v){
        String number = et_number.getText().toString().trim();
        if("".equals(number)){
            Toast.makeText(MainActivity.this, "不能为空", Toast.LENGTH_LONG).show();
            return;
        }
        
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_CALL);
        intent.setData(Uri.parse("tel:"+number));
        startActivity(intent);
    }
}

相关推荐