安卓开发12:安卓各种事件操作
事件:
一般你点击一个按钮,触摸一下屏幕,都是事件。点击,键盘按键按下,鼠标滑过等等。
先看一个OnClick 安卓中的Button的点击事件:
activity_main.xml:android:onClick="Hello" 定义了一个Onclick的点击事件,点击之后,触发MainActivity中的Hello函数。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:text="Button" android:onClick="Hello" /> </RelativeLayout>
MainActivity.java:
package com.example.test.com; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.Window; import android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); } /** * 点击事件 * @param v */ public void Hello(View v) { Toast.makeText(getBaseContext(), "YES!", Toast.LENGTH_SHORT).show(); //一个弹出框 } }
效果图:
换一种方法:
上面这种方法有点笨拙,每次对一个按钮需要定义一些事件的时候,都需要去更改xml布局文件,这样做比较麻烦也不优雅。我们可以再Java代码端,给按钮添加事件监听。
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:text="Button" /> </RelativeLayout>
MainActivity.java:
package com.example.test.com; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.Window; import android.widget.Button; import android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); //获取Button对象 Button button1 = (Button) findViewById(R.id.button1); //添加点击事件的监听 button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(getBaseContext(), "YES!", Toast.LENGTH_SHORT).show(); } }); } }
OnTouch 点击拖动事件:
package com.example.test.com; import android.app.Activity; import android.os.Bundle; import android.view.MotionEvent; import android.view.View; import android.view.Window; import android.widget.Button; import android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); //获取Button对象 Button button1 = (Button) findViewById(R.id.button1); //Button OnTouch事件 button1.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { int iAction = event.getAction(); if (iAction == MotionEvent.ACTION_MOVE) { Toast.makeText(getBaseContext(), "YES!", Toast.LENGTH_SHORT).show(); } return false; } }); } }
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:text="Button" /> </RelativeLayout>
安卓还有很多很多事件,这些事件具体的都需要自己在实战中去操作和积累经验。用法什么的 其实就和上面的一样的。