android学习笔记之基本事件的响应
2016-07-19 15:18
260 查看
<span style="font-size:24px;">1.描述文件中的内容如下所示: </span>
<span style="font-size:24px;"><?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.helloactivity" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.helloactivity.HelloActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest></span>
<span style="font-size:24px;"></pre><pre name="code" class="html" style="font-size: 18px;">里面主要是activity:</span>
<span style="font-size:24px;"><span style="font-size: 18px; font-family: Arial, Helvetica, sans-serif;"></span><pre name="code" class="html" style="font-size: 18px;"> <activity android:name="com.example.helloactivity.HelloActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity></span>
</pre><pre name="code" class="html"><span style="font-size:24px;">2.布局文件代码片段如下所示:</span>
<span style="font-size:24px;"><pre name="code" class="html" style="font-size: 18px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/screen" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <TextView android:id="@+id/text1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:textSize="24sp" android:text="@string/text1" /> <Button android:id="@+id/button1" android:layout_width="80sp" android:layout_height="wrap_content" android:layout_gravity="center" android:text="@string/red"/> <Butto 4000 n android:id="@+id/button2" android:layout_width="80sp" android:layout_height="wrap_content" android:layout_gravity="center" android:text="@string/green"/> </LinearLayout> </span>
3.行为将在源代码文件实现:
<span style="font-size:24px;"><span style="font-size: 18px; font-family: Arial, Helvetica, sans-serif;">package com.android.basicapp; import android.app.Activity; import android.os.Bundle; import android.graphics.Color; import android.widget.Button; import android.widget.TextView; import android.view.View; import android.view.View.OnClickListener; import android.util.Log; public class TestEvent1 extends Activity { private static final String TAG = "TestEvent1"; public TestEvent1() { } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.testevent); final TextView Text = (TextView) findViewById(R.id.text1); // 获得句柄 final Button Button1 = (Button) findViewById(R.id.button1); final Button Button2 = (Button) findViewById(R.id.button2); Button1.setOnClickListener(new OnClickListener() { // 实现行为功能 public void onClick(View v) { Text.setBackgroundColor(Color.RED); } }); Button2.setOnClickListener(new OnClickListener() { public void onClick(View v) { Text.setBackgroundColor(Color.GREEN); } }); } } </span>
<span style="font-size:24px; font-family: Arial, Helvetica, sans-serif;"> </span>
<span style="font-size:24px; font-family: Arial, Helvetica, sans-serif;">通过 findViewById 获得各个屏幕上面的控件(控件)的背景,这里使用的 R.id.button1 等和</span><span style="font-size:24px; font-family: Arial, Helvetica, sans-serif;">布局文件中各个元素的 id 是对应的。</span>
<span style="font-size:24px;">使用方面,这两个编程方面要点是: 使用 findViewById()获取布局文件(XML)中控件的句柄; 使用 setOnXXXListener()设置事件处理函数。 </span>
相关文章推荐
- Android中跨进程通信的四种方式
- Android 代码隐藏手机键盘
- 为Android系统或你的应用提供搜索功能--开始使用搜索功能
- Error:(34) Error: "xxx" is translated here but not found in default locale [ExtraTrans
- Android跨进程通信一 Messenger
- 为Android系统或你的应用提供搜索功能
- Android开发,ListView的OnItemClickListener被里面的Button的OnClickListener拦截
- android的Snackbar使用入门
- Android中跨进程通信方式之使用Bundle
- Android中跨进程通信方式之使用文件共享
- 控件收集
- 自定义android用户控件,使用回调函数实现自定义事件
- Android 上的数据格式 FlatBuffers
- Android Volley框架的简单get和post demo
- 获取Android应用专属缓存存储目录
- Android 进程常驻(0)----MarsDaemon使用说明
- EditText详细介绍
- Android:如何从堆栈中还原ProGuard混淆后的代码
- Android自定义控件:进度条的四种实现方式
- Android平板上开发应用的一点心得——精确适配不同的dpi和屏幕尺寸