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.