zoukankan      html  css  js  c++  java
  • android开发------响应用户事件

    今天的内容有点简单,不难,就是为按钮添加onClick事件。

     新知识点:

      Intent类的简单使用

      startActivity方法

    一般事件都由按钮触发,现在我们要实现的是当用户点击按钮的时候,启动一个新的Activity

    首先建立一个简单的线性布局,代码如下:

     1 <LinearLayout
     2     android:layout_width="fill_parent"
     3     android:layout_height="fill_parent"
     4     android:orientation="vertical" 
     5     xmlns:android="http://schemas.android.com/apk/res/android">
     6     
     7     <Button 
     8         android:id="@+id/btnStart"
     9         android:layout_width="fill_parent"
    10         android:layout_height="wrap_content"
    11         android:text="Click me"
    12         android:onClick="onClickStartActivity"/>
    13 </LinearLayout>
    View Code

    布局中我们只添加了一个按钮,当点击这个按钮,一个新的Activity就会启动。

    这次我们在按钮中添加了一个新的属性:

    android:onClick="onClickStartActivity"

    很明显,这个属性就是指定一个当用户点击的时候程序执行的事件。

    双引号中是我们将来要在Activity中添加的函数。

    接下来创建一个新的Activity,我们依然采用的是手动创建的方式,这个就是以后被onClick事件启动的Activity

    先建立一个继承自Activity的类,名称随意,能清晰表达就行

     1 package com.aidevelops.onclickevent;
     2 
     3 import android.app.Activity;
     4 import android.os.Bundle;
     5 
     6 public class TargetActivity extends Activity {
     7 
     8     @Override
     9     protected void onCreate(Bundle savedInstanceState) {
    10         // TODO Auto-generated method stub
    11         super.onCreate(savedInstanceState);
    12         setContentView(R.layout.targetactivity);
    13     }
    14 }
    View Code


    为这个Activity编写一个界面

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     android:layout_width="match_parent"
     4     android:layout_height="match_parent"
     5     android:orientation="vertical" >
     6     
     7 
     8     <TextView 
     9         android:text="Started by the main activity"
    10         android:layout_width="fill_parent"
    11         android:layout_height="wrap_content"
    12         android:gravity="center"
    13         android:textSize="18sp"/>
    14 </LinearLayout>
    View Code


    记得在AndroidManifest中注册这个Activity,在application标签中添加下面的代码:

    1 <activity 
    2             android:name=".TargetActivity"
    3             android:label="The target activity">
    4             <intent-filter >
    5                 <action android:name="com.aidevelops.onclickevent.TargetActivity" />
    6                 <category android:name="android.intent.category.DEFAULT" />
    7             </intent-filter>
    8         </activity>
    View Code

    前面我们说过AndroidManifest文件中Activity必须包含两个标签,缺一不可:

    这里action中的值我们使用了自定义的类:TartgetActivity,为了确保唯一性,我们在类前面添加了包名

    而category的值是android.intent.category.DEFAULT,因为我们不需要让它出现在应用程序列表中,

    我们现在只需要记住:

    startActivity()要找到一个Activity进行启动,那么这个Activity必须至少包含一个category值,这个category值使用android.intent.category.DEFAULT就可以了。

    准备工作都做好了,接下来就要编写onClickStartActivity函数实质代码

     1 package com.aidevelops.onclickevent;
     2 
     3 import android.app.Activity;
     4 import android.content.Intent;
     5 import android.os.Bundle;
     6 import android.view.View;
     7 
     8 public class MainActivity extends Activity {
     9 
    10     @Override
    11     protected void onCreate(Bundle savedInstanceState) {
    12         super.onCreate(savedInstanceState);
    13         setContentView(R.layout.activity_main);
    14     }
    15     
    16     public void onClickStartActivity(View view)
    17     {
    18         Intent intent = new Intent("com.aidevelops.onclickevent.TargetActivity");
    19         startActivity(intent);
    20     }
    21 
    22 }
    View Code

     在MainActivity中我们新添加了一个函数onClickStartActivity,关于这个函数,我们要注意几点:

    1,函数名称必须要和布局文件中android:onClick属性定义的名称一致,否则点击按钮的时候程序就会崩溃。

    2,函数必须是公共的

    3,函数必须没有返回值

    4,函数必须接收一个View类型的参数

    先来说说Intent的作用

      Intent翻译成中文,就是意图的意思,也就是说,我们想要做什么,就告诉这个Intent.

    现在我们将字符串值"com.aidevelops.onclickevent.TargetActivity"传给Intent,就是告诉它我们要启动这个Activity,你要帮我找到它。

    然后使用startActivity(intent)启动这个Activity。

    使用Eclipse的朋友,只需要按快捷键atrl+shift+O就可以自动导入需要的类,方便快捷。

      写到这里,全部的工作就告一段落,将这个程序部署到虚拟机测试就可以了。你会发现,当你点击按钮,TargetActivity就会被启动。

    真的,就这么简单。图就不截了。

  • 相关阅读:
    基于Swoole的HTTP/HTTPS代理
    Java9新特性系列(module&maven&starter)
    Java9新特性系列(module&maven&starter)
    Java9新特性系列(module&maven&starter)
    Java9新特性系列(module&maven&starter)
    RxJava2源码解析(二)
    C#中的委托和事件(续)
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
  • 原文地址:https://www.cnblogs.com/ai-developers/p/android_click_event.html
Copyright © 2011-2022 走看看