安卓案例1:拨号器
package org.dreamtech.helloworld; import android.net.Uri; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { private EditText et_number; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_number = (EditText) findViewById(R.id.editText1); Button btn_call = (Button) findViewById(R.id.button1); btn_call.setOnClickListener(new MyClickListener()); } private class MyClickListener implements OnClickListener{ @Override public void onClick(View v) { //get String number = et_number.getText().toString().trim(); if("".equals(number)){ Toast.makeText(MainActivity.this, "不能为空", Toast.LENGTH_LONG).show(); return; } Intent intent = new Intent(); intent.setAction(Intent.ACTION_CALL); intent.setData(Uri.parse("tel:"+number)); startActivity(intent); } } }
注意设置权限:
布局:
上边采用的是按钮点击事件第一种:内部类
第二种:匿名内部类
btn_call.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub } });
第三种:实现接口(多按钮时候推荐使用)
package org.dreamtech.helloworld; import android.net.Uri; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity implements OnClickListener { private EditText et_number; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_number = (EditText) findViewById(R.id.editText1); Button btn_call = (Button) findViewById(R.id.button1); btn_call.setOnClickListener(this); } @Override public void onClick(View v) { String number = et_number.getText().toString().trim(); if("".equals(number)){ Toast.makeText(MainActivity.this, "不能为空", Toast.LENGTH_LONG).show(); return; } Intent intent = new Intent(); intent.setAction(Intent.ACTION_CALL); intent.setData(Uri.parse("tel:"+number)); startActivity(intent); } }
第四种:声明方法
<Button android:onClick="click" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/editText1" android:text="拨打" />
package org.dreamtech.helloworld; import android.net.Uri; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { private EditText et_number; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_number = (EditText) findViewById(R.id.editText1); Button btn_call = (Button) findViewById(R.id.button1); } public void click(View v){ String number = et_number.getText().toString().trim(); if("".equals(number)){ Toast.makeText(MainActivity.this, "不能为空", Toast.LENGTH_LONG).show(); return; } Intent intent = new Intent(); intent.setAction(Intent.ACTION_CALL); intent.setData(Uri.parse("tel:"+number)); startActivity(intent); } }
相关推荐
huha 2020-10-16
xfcyhades 2020-11-20
sgafdsg 2020-11-04
Michael 2020-11-03
fengyeezju 2020-10-14
ziyexiaoxiao 2020-10-14
业余架构师 2020-10-09
OuNuo0 2020-09-29
moses 2020-09-22
Angelia 2020-09-11
qinxu 2020-09-10
刘炳昭 2020-09-10
Nostalgiachild 2020-09-07
Nostalgiachild 2020-08-17
leavesC 2020-08-14
一青年 2020-08-13
AndroidAiStudy 2020-08-07
ydc0 2020-07-30
绿豆饼 2020-07-28