//按钮Activity实现接口
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);
Button btn2 = (Button) findViewById(R.id.button2);
Button btn3 = (Button) findViewById(R.id.button3);
btn_call.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// View v 参数 当控件被点击的时候 被点击的对象就会作为参数传进来 可以通过控件的id进行区分
int id = v.getId();
switch (id) {
case R.id.button1:
String number = et_number.getText().toString().trim();
if (TextUtils.isEmpty(number)) {
Toast.makeText(this, "输入不能为空", Toast.LENGTH_SHORT).show();
} else {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + number));
startActivity(intent);
}
break;
case R.id.button2:
Toast.makeText(this, "按钮2被点击", Toast.LENGTH_SHORT).show();
break;
case R.id.button3:
Toast.makeText(this, "按钮3被点击", Toast.LENGTH_SHORT).show();
break;
}
}
}