安卓入门小程序

一:程序控制文本赋值
接着上节修改工程文件,在配置文件中添加一个文本和按钮

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.hello.MainActivity$PlaceholderFragment" >

     <TextView 
        android:id="@+id/mytext"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"

        />
    <Button 
        android:id="@+id/mybut"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />


</LinearLayout>

Java代码中修改onCreate函数

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_main);
         TextView text =(TextView)super.findViewById(R.id.mytext);
            text.setText("SZU");
            Button but = (Button)super.findViewById(R.id.mybut);
            but.setText("Click");

    }

R.layout.fragment_main 使用的是当前配置的布局注意对应
使用findviewbyId方法获取组件,并对组件的文本赋值

*一个安卓程序由xml文件配置组件和java程序取得组件。
二:配置文件为组件文本赋值
上面的文本信息通过程序赋值,也可以直接写在string.xml中进行配置读取
1.在string.xml中添加两个字符串字段

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Hello</string>
    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>
    <string name="school_name">SZU</string>
    <string name="buttonInfo">Click</string>

</resources>

2.通过布局文件将字段关联


     <TextView 
        android:id="@+id/mytext"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text ="@string/school_name"
        />
    <Button 
        android:id="@+id/mybut"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text ="@string/buttonInfo"
        />

3.删去java程序中对文本赋值的操作

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_main);
    }

最终结果与之前一致。
三:以上的布局管理器都是通过配置文件生成的,也可以直接通过程序来实例化这些布局。
修改java源程序

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.fragment_main);
        LinearLayout layout = new LinearLayout(this);
        layout.setOrientation(LinearLayout.VERTICAL);
        TextView text = new TextView(this);
        text.setText(super.getString(R.string.school_name));
        Button button = new Button(this);
        button.setText(super.getString(R.string.buttonInfo));
        layout.addView(text);
        layout.addView(button);
        super.setContentView(layout);

    }

程序实例化布局管理器,就可以不用指定布局配置文件,代码比较简单,实例化一个线性的布局管理器,垂直摆放,添加组件,最后设置默认布局管理器。

在Android的开发中,所有组件既可以通过配置的形式完成定义,也可以利用程序生成。
项目源码:http://download.csdn.net/detail/fatestay_dc/9110185

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xxpr_ybgg

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值