- package com.example.student.myapplication;
- import android.Manifest;
- import android.content.Intent;
- import android.content.pm.PackageManager;
- import android.net.Uri;
- import android.support.v4.app.ActivityCompat;
- import android.support.v4.content.ContextCompat;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.TextView;
- import static android.Manifest.permission.*;
- public class MainActivity extends AppCompatActivity {
- Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b0,star,holl,dial,delete;
- TextView output;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- Init();
- }
- Button.OnClickListener listener = new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Button tmp = (Button) findViewById(v.getId());
- output.setText(output.getText().toString() + tmp.getText().toString());
- }
- };
- public void Init(){
- b1=(Button)findViewById(R.id.button);
- b1.setOnClickListener(listener);
- b2=(Button)findViewById(R.id.button2);
- b2.setOnClickListener(listener);
- b3=(Button)findViewById(R.id.button3);
- b3.setOnClickListener(listener);
- b4=(Button)findViewById(R.id.button4);
- b4.setOnClickListener(listener);
- b5=(Button)findViewById(R.id.button5);
- b5.setOnClickListener(listener);
- b6=(Button)findViewById(R.id.button6);
- b6.setOnClickListener(listener);
- b7=(Button)findViewById(R.id.button7);
- b7.setOnClickListener(listener);
- b8=(Button)findViewById(R.id.button8);
- b8.setOnClickListener(listener);
- b9=(Button)findViewById(R.id.button9);
- b9.setOnClickListener(listener);
- b0=(Button)findViewById(R.id.button0);
- b0.setOnClickListener(listener);
- star=(Button)findViewById(R.id.star);
- star.setOnClickListener(listener);
- holl=(Button)findViewById(R.id.holl);
- holl.setOnClickListener(listener);
- delete=(Button)findViewById(R.id.delete);
- delete.setOnClickListener(new View.OnClickListener(){
- @Override
- public void onClick(View v) {
- if(!output.getText().toString().equals(""))
- output.setText(output.getText().toString().substring(0, output.getText().toString().length()-1));
- }
- });
- delete.setOnLongClickListener(new View.OnLongClickListener() {
- public boolean onLongClick(View v) {
- output.setText("");
- return true;
- }
- });
- dial=(Button)findViewById(R.id.dial);
- output=(TextView)findViewById(R.id.output);
- }
- public void PhoneCall(View view){
- try {
- if (ContextCompat.checkSelfPermission(getApplicationContext(), CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
- Intent dial = new Intent();
- dial.setAction("android.intent.action.CALL");
- dial.setData(Uri.parse("tel:" + output.getText().toString()));
- startActivity(dial);
- } else {
- requestPermissions(new String[]{CALL_PHONE}, 1);
- }
- }catch(Exception e)
- {
- output.setText(e.toString());
- }
- }
- }
複製代碼 |