04项目的目录结构分析与资源引用

本文通过一个具体的Android应用实例,详细介绍了如何使用Android中的资源文件(如strings.xml)来管理应用内的字符串资源,并展示了如何在布局文件中引用这些资源。此外,还提供了Activity中响应按钮点击事件并更新TextView内容的具体实现。

普通字符串资源:

 

布局资源:

 

案例:

main.xml:

<TextView

        android:id="@+id/textView1"

        android:layout_width="match_parent"

        android:layout_height="240dp"

        android:text="加油吧!少年!"

        />

    <Button

        android:id="@+id/button1"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:text="@string/msg"

        android:onClick="test"

        />

R.java:

public final class R {

    public static final class attr {

    }

    public static final class drawable {

        public static final int ic_launcher=0x7f020000;

    }

    public static final class id {

        public static final int button1=0x7f050001;

        public static final int textView1=0x7f050000;

    }

    public static final class layout {

        public static final int main=0x7f030000;

    }

    public static final class string {

        public static final int app_name=0x7f040000;

        public static final int hello=0x7f040001;

        public static final int msg=0x7f040002;

    }

}

 

Strings.xml:

<resources>

    <string name="app_name">ONEAndroid</string>

    <string name="hello">Hello World, ONEAndroidActivity!</string>

    <string name="msg">welcome,weini!</string>

</resources>

 

ONEAndroidActivity.java:

public void test(View view){

     TextView textView1=(TextView) findViewById (R.id.textView1);

     textView1.setText("The greatest test of courage on earth is to bear defeat without losing heart\n世界上对勇气的最大考验是忍受失败而不丧失信心。");

     Toast.makeText(ONEAndroidActivity.this, "点击button",Toast.LENGTH_SHORT).show();

}

 

运行结果:

点击按钮前:

 

  点击按后:

 

中英文时候的选择:

 

 

图示:

 

 

转载于:https://www.cnblogs.com/cxm-weiniss/p/7193103.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值