Call the android sub-function

Source: Internet
Author: User

Call the android sub-function
Create an Android project.

1 Layout
Open main. xml and modify the content as follows:

<TextView  android:layout_width="fill_parent"  android:layout_height="wrap_content"  android:text="@string/mobile"/>  <EditText   android:layout_width="fill_parent"  android:layout_height="wrap_content"  android:inputType="text"  android:id="@+id/mobile"  /><Button   android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:text="@string/button"  android:id="@+id/button"  />






Binary definition string
Open strings. xml and add the following content:
<String name = "mobile"> enter the mobile phone number </string> <string name = "button"> dialing </string>


3. Response to click events
1. method 1 (recommended)
Open MainActivity. java and add the following code to the onCreate function:
public void onCreate(Bundle savedInstanceState){  super.onCreate(savedInstanceState);setContentView(R.layout.main);Button button = (Button) this.findViewById(R.id.button);button.setOnClickListener(new ButtonClick());}

Define an internal class to implement the callback object:
private final class ButtonClick implements View.OnClickListener{public void onClick(View v){EditText text = (EditText) findViewById(R.id.mobile);String num = text.getText().toString();Intent intent = new Intent();intent.setAction("android.intent.action.CALL");intent.setData(Uri.parse("tel:"+num));startActivity(intent);}}

Internal classes reduce file loading and speed up Virtual Machine Software loading.


2 method 2
public void onCreate(Bundle savedInstanceState){  super.onCreate(savedInstanceState);setContentView(R.layout.main);Button button = (Button) this.findViewById(R.button);button.setOnClickListener(new View.OnClickListener(){public void onClick(View v){EditText text = (EditText) findViewById(R.id.mobile);String num = text.getText().toString();Intent intent = new Intent();intent.setAction("android.intent.action.CALL");intent.setData(Uri.parse("tel:"+num));startActivity(intent);}});}


4. Add Permissions
Add permissions to Manifest. xml:
<uses-permission android:name="android.permission.CALL_PHONE"/> 

5. Optimization
Each time you click a button, onClick is executed and the code is executed:
EditText text = (EditText) findViewById(R.id.mobile);
To search for controls and find resources, so we can put this sentence in onCreate and only execute it once.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.