Android phone dialer

I learned before 3. Introduction to the helloworld project structure of android-adt After that, this chapter will write a simple telephone dialer program

The implementation steps are as follows:

  • 1. Create project
  • 2. Write layout / activity main.xml layout file (draw ui) to achieve display effect
  • 3. Write MainActivity.java code to realize specific logic functions
  • 4. Start APP to see the effect

 

1. Create project

After setting the project name, click next until finish

The layout / activity main.xml layout interface created is as follows:

 

The corresponding layout xml is as follows:

<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_horizontal_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

 
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>

We can see that as long as the values of the layout'width and layout'height attributes are direct values, all the others are marked with index (@ XXX/YYYY). These index pointer addresses will exist in R.java, and the corresponding index contents will exist in values/XXXs.xml (for example, @ dimen is located in dimens.xml, and string is located in strings.xml)

Take @ string/hello_world as an example:

In R.java

 

In values/strings.xml

 

So the TextView control shows hello world!

Why index is needed instead of writing it directly as: android:text="hello world!"

Answer: in order to internationalize the language, because the value pointed to by the index can be replaced, we use the index content under values/xxx.xml by default. If we set it to English, we can change it to the English index content under values-en/xxx.xml (learn in the internationalization chapter later)

But if we write directly, we can't realize language internationalization

Analyze the property value of RelativeLayout

  • RelativeLayout: it means relative layout. It means to set the current interface as relative layout
  • Android: layout? Width and Android: layout? Height: set the width and height of the layout. For the interface layout, only "match? Parent" can be set here.
  • android:paddingXXX: inner border indicates how far the control inside the interface layout is from its edge
  • tools:context=".MainActivity": indicates that this xml layout file is associated with src/MainActivity.java class (used to implement specific logical functions)

Analyze the property value of TextView

TextView controls for displaying regular text that cannot be modified

Note: to display the control, you must specify layout? Width and layout? Height

There are three values to fill in for the width and height settings of the control, as shown in the following figure:

 

"fill_parent"   //Force it to fill the blank space of the parent control

"wrap_content" //Set the height of the control just enough to display the size of the current content

"match_parent" //and fill_parent That's what it means,Their definitions are essentially the same,All are-1,It's just a change of alias, in order to name it more accurately, Official recommendation match_parent

 

2. Write layout / activity main.xml layout file (draw ui)

The final interface is as follows:

 

 

3. Write MainActivity.java code

In MainActivity.java, a MainActivity class is mainly created and inherited from Activity, as shown in the following figure:

 

Next, we will introduce Activity

3.1 Activity introduction

An Activity is a single, centralized thing that users can do. Almost all activities interact with users, so the Activity class is responsible for creating a window for you where you can place a UI with setContentView(View). Although activities are usually presented to users as full screen windows, they can also be used in other ways: as floating windows (Themes set through windowsfloat) or embedded in another Activity (using activitygroups). There are two methods that almost all subclass activities will implement:

  • onCreate(Bundle): where the activity is initialized, it will be called when the activity is created. Here, setContentView(int) is usually called using the layout resource defining the UI, and findViewById(int) is used to retrieve the widgets in the UI that need to interact programmatically.
  • onPause(): is where the user leaves the activity. Most importantly, any changes made by the user at this time should be submitted (usually to the content provider that holds the data).

 

3.2 write MainActivity.java

package com.example.phonecall;
import android.net.Uri;
import android.os.Bundle; 
import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

 
//Add inheritance OnClickListener Click event class,Then overwrite onClick()
public class MainActivity extends Activity  implements OnClickListener {
    private EditText et_number;        //Text box object

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); //Load the interface to display UI
       //1.find EditText,Then set the key press to monitor
       et_number = (EditText) findViewById(R.id.editText1);
       Button bt_call = (Button) findViewById(R.id.button1);
       bt_call.setOnClickListener(MainActivity.this);  //MainActivity.this Express MainActivity class

    }


    @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); //When clicking the menu button,The menu interface will be displayed
        return true;
    }

    @Override
    public void onClick(View v) {
        System.out.println("on click");

        String number = et_number.getText().toString().trim();    //2.Get content

        if(TextUtils.isEmpty(number)){
        Toast.makeText(MainActivity.this, "Content cannot be empty", Toast.LENGTH_SHORT).show(); //Floating display block displaying a prompt

        }else{

            Intent intent = new Intent();
            intent.setAction(intent.ACTION_CALL);      //Set up intent Action to perform, ACTION_CALL Indicates a call
            intent.setData(Uri.parse("tel:"+number));  //uri and url Similar,however uri Multiple protocols can be customized,"tel:"+number Express to number Phone
            startActivity(intent);                     //adopt intent(intention)To realize the interaction and communication between applications,So as to realize people's intention
        }
    }
}

Note: when importing a package and clicking the package listening to an event, select the second "android.view.view":

 

Otherwise, onClick(View v) will become onClick(DialogInterface dialog,int which).

 

3.3 then add the call request permission (for whether to support direct call from the application)

In the Android manifest list - > permissions, select Users Permission

Then add android.permission.call'phone

 

 

4. Then start the APP

Tags: Android xml Java Windows

Posted on Mon, 06 Jan 2020 06:50:53 -0800 by FadeOut79