一个按钮对点击事件相应的四种方法。
1、通过新建一个借口类来具体实现点击时间的响应:
package com.example.dailcall; import android.support.v7.app.ActionBarActivity; import android.text.TextUtils; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View;//创建 OnClickListener接口的时候要导入这个包 import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends ActionBarActivity { // 定义组建 private EditText et; private Button btn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et=(EditText) findViewById(R.id.et_number); //通过findViewById来查找组建名,绑定到定义的组建变量。 btn=(Button) findViewById(R.id.btn_dail); btn.setOnClickListener(new MyClickListener());//通过新建一个借口类来具体实现点击时间的响应。 } private class MyClickListener implements OnClickListener{ @Override public void onClick(View v) { // TODO Auto-generated method stub callNumber(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } private void callNumber() { Intent intent=new Intent(); String number=et.getText().toString().trim(); if (TextUtils.isEmpty(number)) { Toast.makeText(MainActivity.this, "电话号码不能为空!", 0).show(); return; } intent.setAction(Intent.ACTION_CALL); intent.setData(Uri.parse("tel:"+number)); startActivity(intent); } }
2、通过new一个匿名内部类类实现:
package com.example.dailcall; import android.support.v7.app.ActionBarActivity; import android.text.TextUtils; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View;//创建 OnClickListener接口的时候要导入这个包 import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends ActionBarActivity { // 定义组建 private EditText et; private Button btn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et=(EditText) findViewById(R.id.et_number); //通过findViewById来查找组建名,绑定到定义的组建变量。 btn=(Button) findViewById(R.id.btn_dail); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub callNumber(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } private void callNumber() { Intent intent=new Intent(); String number=et.getText().toString().trim(); if (TextUtils.isEmpty(number)) { Toast.makeText(MainActivity.this, "电话号码不能为空!", 0).show(); return; } intent.setAction(Intent.ACTION_CALL); intent.setData(Uri.parse("tel:"+number)); startActivity(intent); } }
3、让MainActivity来实现点击时间的借口
package com.example.dailcall; import android.support.v7.app.ActionBarActivity; import android.text.TextUtils; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View;//创建 OnClickListener接口的时候要导入这个包 import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Switch; import android.widget.Toast; public class MainActivity extends ActionBarActivity implements OnClickListener { // 定义组建 private EditText et; private Button btn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et=(EditText) findViewById(R.id.et_number); //通过findViewById来查找组建名,绑定到定义的组建变量。 btn=(Button) findViewById(R.id.btn_dail); btn.setOnClickListener(this); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } private void callNumber() { Intent intent=new Intent(); String number=et.getText().toString().trim(); if (TextUtils.isEmpty(number)) { Toast.makeText(MainActivity.this, "电话号码不能为空!", 0).show(); return; } intent.setAction(Intent.ACTION_CALL); intent.setData(Uri.parse("tel:"+number)); startActivity(intent); } @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.btn_dail: callNumber(); break; default: break; } } }
4、在布局文件中button的按钮里边定义一个onclick的属性:
package com.example.dailcall; import android.support.v7.app.ActionBarActivity; import android.text.TextUtils; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View;//创建 OnClickListener接口的时候要导入这个包 import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends ActionBarActivity { // 定义组建 private EditText et; private Button btn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et=(EditText) findViewById(R.id.et_number); //通过findViewById来查找组建名,绑定到定义的组建变量。 btn=(Button) findViewById(R.id.btn_dail); } public void btndail(View view){ callNumber(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } private void callNumber() { Intent intent=new Intent(); String number=et.getText().toString().trim(); if (TextUtils.isEmpty(number)) { Toast.makeText(MainActivity.this, "电话号码不能为空!", 0).show(); return; } intent.setAction(Intent.ACTION_CALL); intent.setData(Uri.parse("tel:"+number)); startActivity(intent); } }
其中button的定义文件为:
<Button android:onClick="btndail" android:id="@+id/btn_dail" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/et_number" android:text="@string/dail" />