android四种点击事件,android点击事件的四种⽅式android点击事件的四种⽅式
第⼀种⽅式:创建内部类实现点击事件
代码如下:
ample.dail;
TextUtils;
import android.app.Activity;
t.Intent;
import android.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
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 etext;
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
/
/获取text⽂本框内容
etext = (EditText) MainActivity.this.findViewById(R.id.editText1);
//根据id获取button按钮
Button btn = (Button) this.findViewById(R.id.button_dail);
//给按钮添加点击时事件(有四种给按钮添加点击事件⽅式:1、创建内部类实现点击事件)
btn.setOnClickListener(new MyOnClickListener());
}
private class MyOnClickListener implements OnClickListener{
@Override
public void onClick(View arg0) {
callphone();android模拟点击
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().u.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = ItemId();
if (id == R.id.action_settings) {
return true;
}
OptionsItemSelected(item);
}
private void callphone() {
String e_number = Text().toString();
if(TextUtils.isEmpty(e_number)) {
Toast.makeText(MainActivity.this, "号码不能为空!", Toast.LENGTH_SHORT).show(); return ;
}
Intent intent = new Intent();
intent.setAction(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:"+e_number));
startActivity(intent);
}
}
第⼆种⽅式:采⽤匿名内部类实现点击事件
ample.dail;
TextUtils;
import android.app.Activity;
t.Intent;
import android.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
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 etext;
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
//获取text⽂本框内容
etext = (EditText) MainActivity.this.findViewById(R.id.editText1);
//根据id获取button按钮
Button btn = (Button) this.findViewById(R.id.button_dail);
//给按钮添加点击时事件(有四种给按钮添加点击事件⽅式:1、创建内部类实现点击事件) // btn.setOnClickListener(new MyOnClickListener());
//第⼆种⽅式:采⽤匿名内部类实现点击事件
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
callphone();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().u.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = ItemId();
if (id == R.id.action_settings) {
return true;
}
OptionsItemSelected(item);
}
private void callphone() {
String e_number = Text().toString();
if(TextUtils.isEmpty(e_number)) {
Toast.makeText(MainActivity.this, "号码不能为空!", Toast.LENGTH_SHORT).show(); return ;
}
Intent intent = new Intent();
intent.setAction(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:"+e_number));
startActivity(intent);
}
}
第三种⽅式:采⽤Activity实现OnClickListener接⼝⽅式来实现点击事件
ample.dail;
TextUtils;
import android.app.Activity;
t.Intent;
import android.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
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 etext;
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
//获取text⽂本框内容
etext = (EditText) MainActivity.this.findViewById(R.id.editText1);
//根据id获取button按钮
Button btn = (Button) this.findViewById(R.id.button_dail);
//给按钮添加点击时事件(有四种给按钮添加点击事件⽅式:1、创建内部类实现点击事件) // btn.setOnClickListener(new MyOnClickListener());
//第⼆种⽅式:采⽤匿名内部类实现点击事件
/*btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
callphone();
}
});*/
//第三种⽅式:采⽤Activity实现OnClickListener接⼝⽅式来实现点击事件
btn.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().u.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = ItemId();
if (id == R.id.action_settings) {
return true;
}
OptionsItemSelected(item);
}
private void callphone() {
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论