布局文件,多个按钮使用一个函数处理的例子
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/btn1"
android:text="快速拨号"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn2"
android:text="加入通讯录"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn3"
android:text="启动电子邮件"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
/>
<Button
android:id="@+id/btn4"
android:text="启动短信"
android:onClick="onClick"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn5"
android:text="打开网页"
android:onClick="onClick"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn6"
android:text="打开地图"
android:onClick="onClick"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn7"
android:text="搜索web资料"
android:onClick="onClick"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
MainActivity的代码,具体的 onClick()方法的实现:
package com.example.mbenben.newotherapp;
import android.app.SearchManager;
import android.content.Intent;
import android.net.Uri;
import android.provider.ContactsContract;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
public MainActivity(){
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn1=findViewById(R.id.btn1);
Button btn2=findViewById(R.id.btn2);
Button btn3=findViewById(R.id.btn3);
Button btn4=findViewById(R.id.btn4);
Button btn5=findViewById(R.id.btn5);
Button btn6=findViewById(R.id.btn6);
Button btn7=findViewById(R.id.btn7);
btn3.setOnClickListener(this);
btn4.setOnClickListener(this);
btn5.setOnClickListener(this);
btn6.setOnClickListener(this);
btn7.setOnClickListener(this);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent it=new Intent();
it.setAction(Intent.ACTION_VIEW);
it.setData(Uri.parse("tel:10086"));
startActivity(it);
}
});
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent it2=new Intent();
it2.setAction(ContactsContract.Intents.Insert.ACTION);
it2.setType(ContactsContract.RawContacts.CONTENT_TYPE);
it2.putExtra(ContactsContract.Intents.Insert.NAME,"移动客服")
.putExtra(ContactsContract.Intents.Insert.PHONE,"10086");
//it.setData(Uri.parse("tel:800"));
startActivity(it2);
}
});
}
public void onClick(View v){
Intent it=new Intent(Intent.ACTION_VIEW);
switch(v.getId()){
case R.id.btn3:
it.setData(Uri.parse("mailto:[email protected]"));
it.putExtra(Intent.EXTRA_CC,
new String[] {"[email protected]"});
it.putExtra(Intent.EXTRA_SUBJECT,"数据已经接受到");
it.putExtra(Intent.EXTRA_TEXT,"你好,\n已收到,谢谢");
break;
case R.id.btn4:
it.setData(Uri.parse("sms:1008611?body=CXLL"));
break;
case R.id.btn5:
it.setData(Uri.parse("http://sina.cn/?from=wap"));
break;
case R.id.btn6:
it.setData(Uri.parse("geo:39,116"));
break;
case R.id.btn7:
it.setAction(Intent.ACTION_WEB_SEARCH);
it.putExtra(SearchManager.QUERY,"新浪网站");
break;
}
startActivity(it);
}
}