1.首先布局文件:一个TextView和两个Button

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent" >
 5 
 6      <TextView 
 7          android:id="@+id/textviewColor"
 8          android:layout_width="match_parent"
 9          android:layout_height="wrap_content"
10          android:textSize="30dp"
11          android:text="@string/textviewColor"/>
12      <Button 
13          android:id="@+id/btnColor"
14          android:layout_width="match_parent"
15          android:layout_height="wrap_content"
16          android:text="@string/btnColor"
17          android:textSize="20dp"
18          android:layout_below="@id/textviewColor"/>
19        <Button 
20          android:id="@+id/btnGreen"
21          android:layout_width="match_parent"
22          android:layout_height="wrap_content"
23          android:text="@string/btnGreen"
24          android:textSize="20dp"
25          android:layout_below="@id/btnColor"/>
26 </RelativeLayout>

2.Activity的java代码

package com.example.layout;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class ColorActivity extends Activity {

    public ColorActivity(){
        
    }
    private Button btnColor=null;
    private Button btnGreen=null;
    private TextView textviewColor=null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_color);
        btnColor=(Button)findViewById(R.id.btnColor);
        //第一种事件的方法
        btnColor.setOnClickListener(
                new OnClickListener() {
                    
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        textviewColor=(TextView)findViewById(R.id.textviewColor);
                        textviewColor.setTextColor(Color.RED);//字的颜色 红色
                        //textviewColor.setBackgroundColor(Color.GREEN);//背景的颜色
                    }
                }
                );

        //第二种
        btnGreen=(Button)findViewById(R.id.btnGreen);
        btnGreen.setOnClickListener(new btnGreenClick ());
    }
    class btnGreenClick implements OnClickListener
    {

        private TextView textviewColor=null;
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if(v.getId()==R.id.btnGreen)
            {
                textviewColor=(TextView)findViewById(R.id.textviewColor);
                textviewColor.setTextColor(Color.GREEN);//变绿色
            }
        }
        
    }
}


本人也是初学。贴上代码 别的也不知道该从何处开始介绍 哈哈。其他的Values/strings里面的值自己补上。

posted on 2012-11-03 14:19  Hello马先生  阅读(302)  评论(0)    收藏  举报