android学习之4种点击事件的响应方式

如题,下面就一一列出对点击事件响应的4种方式:

第一种:内部类的形式:

package com.example.dail;

import android.net.Uri;
import android.os.Bundle;
import android.renderscript.Int2;
import android.text.TextUtils;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
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 Button bt_dail = null;
private EditText et_number = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt_dail = (Button) findViewById(R.id.bt_dail);
et_number = (EditText) findViewById(R.id.et_number);
bt_dail.setOnClickListener(new MyListener());
}
private void callPhone() {
String number = et_number.getText().toString();
if(TextUtils.isEmpty(number)) {
Toast.makeText(MainActivity.this, "号码不能为空", 1).show();
}
Intent intent = new Intent();
intent.setAction(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:"+number));
startActivity(intent);
}
private class MyListener implements OnClickListener{ @Override
public void onClick(View v) {
// TODO Auto-generated method stub
callPhone();
} } }

创建一个内部类MyListener实现OnClickListener接口。

第二种:匿名内部类:

package com.example.dail;

import android.net.Uri;
import android.os.Bundle;
import android.renderscript.Int2;
import android.text.TextUtils;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
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 Button bt_dail = null;
private EditText et_number = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt_dail = (Button) findViewById(R.id.bt_dail);
et_number = (EditText) findViewById(R.id.et_number);
bt_dail.setOnClickListener(new OnClickListener(){
public void onClick(View v){
callPhone();
}
});
}
private void callPhone() {
String number = et_number.getText().toString();
if(TextUtils.isEmpty(number)) {
Toast.makeText(MainActivity.this, "号码不能为空", 1).show();
}
Intent intent = new Intent();
intent.setAction(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:"+number));
startActivity(intent);
}
}

第三种:在xml布局文件中,定义onclick的方式

<Button
android:onClick="btOnClick"
android:id="@+id/bt_dail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/et_number"
android:text="@string/dail"/>"

然后再主类里面实现btOnClick方法,

private void btOnClick() {
callPhone();
}
private void callPhone() {
String number = et_number.getText().toString();
if(TextUtils.isEmpty(number)) {
Toast.makeText(MainActivity.this, "号码不能为空", 1).show();
}
Intent intent = new Intent();
intent.setAction(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:"+number));
startActivity(intent);
}

第四种:让主类实现OnClickListener接口,然后再主类实现未实现的方法

package com.example.dail;

import android.net.Uri;
import android.os.Bundle;
import android.renderscript.Int2;
import android.text.TextUtils;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
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 Button bt_dail = null;
private EditText et_number = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt_dail = (Button) findViewById(R.id.bt_dail);
et_number = (EditText) findViewById(R.id.et_number);
bt_dail.setOnClickListener(this);
}
private void callPhone() {
String number = et_number.getText().toString();
if(TextUtils.isEmpty(number)) {
Toast.makeText(MainActivity.this, "号码不能为空", 1).show();
}
Intent intent = new Intent();
intent.setAction(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:"+number));
startActivity(intent);
}
@Override
public void onClick(View v) {
callPhone();
}
}

如上所述,4种点击事件的响应方式就是如此。



上一篇:CSS让div背景透明


下一篇:iOS给背景添加点击事件