android学习笔记之基本事件的响应

<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"/> 
    <Button 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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值