<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" />
<string name="mobile">请输入手机号</string> <string name="button">拨号</string>
public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main); Button button = (Button) this.findViewById(R.button); button.setOnClickListener(new ButtonClickListener()); }
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); } }
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); } }); }
<uses-permission android:name="android.permission.CALL_PHONE"/>
EditText text = (EditText) findViewById(R.id.mobile);来查找控件,查找很耗资源,所以我们可以将这句放在onCreate中,只执行一次。
原文地址:http://blog.csdn.net/xufeng0991/article/details/43982547