初学Android应用程序——资源文件、文字、字体颜色、文字背景颜色、按钮

Android工程建好后已经生成一个hellowold程序。里面有一个main.xml,这个就是Android的资源文件,就相当于VC++里的.rc文件,里面定义了一整个Activity和里面的控件。Activity可以理解为程序的一个界面。我写好了一个简单的程序:一个界面上有4个按钮,点击后分别改变画面中间的文字内容、颜色、背景颜色、切换到另一个画面。

 

第一个画面:

 

Activity1    

 

定义此界面的main.xml文件内容如下

 



android:id= "@+id/AbsoluteLayout01"
android:layout_width= "fill_parent"
android:layout_height= "fill_parent"
android:background= "@drawable/white"
xmlns:android= "http://schemas.android.com/apk/res/android" >


android:layout_y= "350dip"
android:layout_width="70dip"
android:id="@+id/Button01"
android:text="改变文字" >

android:layout_y= "350dip"
android:layout_x= "76dip"
android:layout_height= "60dip"
android:layout_width= "70dip"
android:id= "@+id/Button02"
android:text= "改变文字颜色" >

android:layout_y= "350dip"
android:layout_x= "149dip"
android:layout_height= "60dip"
android:layout_width= "70dip"
android:id= "@+id/Button03"
android:text= "改变文字背景" >

android:layout_y= "350dip"
android:layout_x= "222dip"
android:layout_height= "60dip"
android:layout_width= "70dip"
android:id= "@+id/Button04"
android:text= "改变窗口背景" >



android:id= "@+id/TextView01"
android:layout_y= "128dip"
android:layout_x= "95dip"
android:layout_height= "wrap_content"
android:layout_width= "wrap_content"
android:text= "@string/movie1" >


 

 

 

第二个画面:

当点击“返回”后返回第一画面,点击“退出”结束整个程序。

 

第二个界面

 

定义此界面的.xml文件内容如下

 

android:id= "@+id/AbsoluteLayout01"
android:layout_width= "fill_parent"
android:layout_height= "fill_parent"
xmlns:android= "http://schemas.android.com/apk/res/android" >

android:id= "@+id/Button01"
android:text= "返回"
android:layout_y= "350dip"
android:layout_x= "76dip"
android:layout_height= "60dip"
android:layout_width= "70dip" >

android:id= "@+id/Button02"
android:text= "退出"
android:layout_y= "350dip"
android:layout_x= "149dip"
android:layout_height= "60dip"
android:layout_width= "70dip" >


 

 

 

 

 

 

java程序如下:

package textview . beeboobeeboo . cn ;

import android.app.Activity ;
import android.os.Bundle ;
import android.view.View ;
import android.widget.Button ;
import android.widget.TextView ;
import android.graphics.Color ;
import android.graphics.drawable.Drawable ;
import android.content.res.Resources ;

public class textview extends Activity {
    private Button mButton1 ;
    private Button mButton2 ;
    private Button mButton3 ;
    private Button mButton4 ;
    private TextView mTextView ;
    private Resources resources ;
    private Drawable HippoDrawable_green ;
    private Drawable HippoDrawable_blue ;
    /** Called when the activity is first created. */
    @Override
    public void onCreate (Bundle savedInstanceState ) {
        super . onCreate (savedInstanceState );
        showLayout1 ();//显示第一个画面
    }
  
    public void showLayout1 (){
        setContentView (R . layout . main );

        //改变文字内容按钮
        mButton1 = (Button ) findViewById (R . id . Button01 );
        mTextView = (TextView )findViewById (R . id . TextView01 );
        mButton1 . setOnClickListener (new Button . OnClickListener ()
        {
            public void onClick (View v )
            {

                //java中比较字符串必须用equals(),直接用“==”的话比较的是变量的地址,

                //而且内容相同字符串的变量会是同一个地址,真神奇

                //equals函数中获取资源string.xml中的字符串使用用getResources().getString()
                if (mTextView . getText (). equals (getResources (). getString (R . string . movie2 )))
                {
                    mTextView . setText (R . string . movie1 );//这里却不用getResources().getString()也可以
                }
                else
                {
                    mTextView . setText (getResources (). getString (R . string . movie2 ));//用也可以,不明白为什么
                }
            }
        });
        //改变文字颜色按钮
        mButton2 = (Button ) findViewById (R . id . Button02 );
        mButton2 . setOnClickListener (new Button . OnClickListener ()
        {
            //@Override
            public void onClick (View v )
            {
                if (mTextView . getCurrentTextColor ()== Color . RED )//获取color.xml中定义的颜色值
                {
                    mTextView . setTextColor (Color . MAGENTA );//设置字体的颜色
                }
                else
                {
                    mTextView . setTextColor (Color . RED );
                }
            }
        });
        //改变文字背景颜色按钮
        mButton3 = (Button ) findViewById (R . id . Button03 );
        resources = getBaseContext (). getResources ();
        HippoDrawable_green = resources . getDrawable (R . drawable . green );
        HippoDrawable_blue = resources . getDrawable (R . drawable . blue );
        mTextView . setBackgroundDrawable (HippoDrawable_blue );
        mButton3 . setOnClickListener (new Button . OnClickListener ()
        {
            public void onClick (View v )
            {
                if (mTextView . getBackground (). equals (HippoDrawable_green ))//读取当前的背景颜色进行比较
                {
                    mTextView . setBackgroundDrawable (HippoDrawable_blue );
                }
                else
                {
                    mTextView . setBackgroundDrawable (HippoDrawable_green );
                }
            }
        });
        //切换画面按钮
        mButton4 = (Button ) findViewById (R . id . Button04 );
        mButton4 . setOnClickListener (new Button . OnClickListener ()
        {
            //@Override
            public void onClick (View v )
            {
                showLayout2 ();//显示第二个画面
            }
        });
    }
  
    public void showLayout2 (){
        setContentView (R . layout . secondary );//读取第二个画面的.xml文件
        mButton1 = (Button )findViewById (R . id . Button01 );
        mButton1 . setOnClickListener (new Button . OnClickListener ()
        {
            public void onClick (View v )
            {
                showLayout1 ();//显示第一个画面
            }
        });
        mButton2 = (Button )findViewById (R . id . Button02 );
        mButton2 . setOnClickListener (new Button . OnClickListener ()
        {
            public void onClick (View v )
            {
                finish ();//结束程序
            }
        });
    }
}

 

其他用到的资源文件:

定义textview中用到的字符串string.xml:



    name= "movie1" > 大内密探零零狗
    name= "movie2" > 哈利波特与魔法石
    name= "app_name" > textview

 

定义颜色的color.xml:



  name= "darkgray" > #808080FF
  name= "white" > #FFFFFFFF
  name= "green" > #9dc774
  name= "blue" > #6a82d0

 

初学Andorid和java,见笑了


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部