Android点击事件十分常用。点击事件可用于控件,也可用于布局。开发者可以使用点击事件实现弹框、跳转等N多种操作,而实现点击事件也有多种方式。

一、控件的单击事件:

1、setOnClickListener方法

先看布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <Button        android:id="@+id/button"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="button" /></RelativeLayout>
再看Activity文件
package com.example.csdn;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.Toast;public class MainActivity extends Activity {private Button button;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);button = (Button) findViewById(R.id.button);// 实现点击事件button.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Toast.makeText(MainActivity.this, "点击成功", Toast.LENGTH_SHORT).show();}});}}

2、onClick方法:

在XML文件中,在控件的声明时,添加onClick属性,在onClick属性添加的名称method,在对应的Activity中声明public void method(View view){},即可实现。
先看布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:onClick="buttonClick"        android:text="button" /></RelativeLayout>
再看Activity文件
package com.example.csdn;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.Toast;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}/** * button的点击事件 *  * @param view */public void buttonClick(View view) {Toast.makeText(MainActivity.this, "点击成功", Toast.LENGTH_SHORT).show();}}
备注:TextView也可以用此方法实现点击事件,不过需要在XML中添加clickable=true方可起效。如下图:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:clickable="true"        android:onClick="textviewClick" /></RelativeLayout>

3、实现OnClickListener:

在相应的Activity中实现OnClickListener,在默认添加的onClick方法中,即可实现。
先看布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <Button        android:id="@+id/button"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="button" /></RelativeLayout>
再看Activity文件
package com.example.csdn;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Toast;public class MainActivity extends Activity implements OnClickListener {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.button:Toast.makeText(MainActivity.this, "点击成功", Toast.LENGTH_SHORT).show();break;default:break;}}}

二、ListView和GridView的单击事件:

先看布局文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <ListView        android:id="@+id/listview"        android:layout_width="match_parent"        android:layout_height="wrap_content" /></RelativeLayout>
再看Activity文件:
package com.example.csdn;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.ListView;public class MainActivity extends Activity {private ListView listview;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);listview = (ListView) findViewById(R.id.listview);listview.setOnItemClickListener(new OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {// TODO Auto-generated method stub}});}}

更多相关文章

  1. android上如何写配置文件
  2. 菜单之二:使用xml文件定义菜单
  3. Android中控件的onClickListener,点击监听器
  4. Android中sqllite存储海量数据解决办法
  5. Android(安卓)字体大小怎么自适应不同分辨率?
  6. Android(安卓)开发相关命令集合
  7. android打包so文件到apk
  8. Android(安卓)Studio开发NDK用C++的坑
  9. Android之数据存储笔记

随机推荐

  1. 自定义方法通过类名获取对象集合
  2. HTML基础知识
  3. 实用攻略:云服务器如何选购?要考虑哪些因素
  4. Python MNIST手写体识别详解与试练
  5. 0701作业
  6. 固定导航栏
  7. 【阿里云镜像】使用阿里巴巴开源镜像站镜
  8. 聊一聊如何把SSL证书安装到小鸟云服务器
  9. 服务器登录密码忘记了怎么办?总结办法如下
  10. 阿萨德请问日期为人