Android点击事件的实现
16lz
2021-01-26
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}});}}
更多相关文章
- android上如何写配置文件
- 菜单之二:使用xml文件定义菜单
- Android中控件的onClickListener,点击监听器
- Android中sqllite存储海量数据解决办法
- Android(安卓)字体大小怎么自适应不同分辨率?
- Android(安卓)开发相关命令集合
- android打包so文件到apk
- Android(安卓)Studio开发NDK用C++的坑
- Android之数据存储笔记