首页 | 新闻 | 新品 | 文库 | 方案 | 视频 | 下载 | 商城 | 开发板 | 数据中心 | 座谈新版 | 培训 | 工具 | 博客 | 论坛 | 百科 | GEC | 活动 | 主题月 | 电子展
返回列表 回复 发帖

使用 Android 和 XML 构建动态用户界面(7)

使用 Android 和 XML 构建动态用户界面(7)

组装一个用户界面首先创建一个表单以便移动用户输入数据。
从顶端获取应用程序的入口点驻留在 XmlGui.java 中,如  所示。
清单 4. 应用程序入口点:XmlGui
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package com.msi.ibm;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Button;
import android.widget.TextView;
import android.content.Intent;
import android.util.Log;
public class XmlGui extends Activity {
        final String tag = XmlGui.class.getName();
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
         
        Button btnRunForm = (Button) this.findViewById(R.id.btnRunForm);
        btnRunForm.setOnClickListener(new Button.OnClickListener()
        {
           public void onClick(View v)
           {
                       EditText formNumber = (EditText) findViewById(R.id.formNumber);
                       Log.i(tag,"Attempting to process Form #
                   [" + formNumber.getText().toString() + "]");
                       Intent newFormInfo = new Intent(XmlGui.this,RunForm.class);
                       newFormInfo.putExtra("formNumber",
                   formNumber.getText().toString());
                       startActivity(newFormInfo);
           }
        });
    }
}




这个主 Activity 的用户界面非常简单,只包括:
  • 一个标签(TextView)
  • 一个输入框(EditText)
  • 一个按钮(Button)
XmlGui Activity 的代码本质上非常标准。您扩展了设计时创建的一个 layout,然后定义并创建一个按钮处理程序来实先预期的功能(稍后将进一步解释)。
这个用户界面在文件 main.xml(位于 res 文件夹下的 layout 子文件夹中)中定义。 展示了 main.xml。
清单 5. main.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    androidrientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >

   <TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="@string/Title"
       />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    androidrientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >

   <EditText
       android:layout_width="100px"
       android:layout_height="wrap_content"
       android:text="1"
       android:id="@+id/formNumber"
   android:numeric="integer"/>
   <Button android:text="Run Form" android:id="@+id/btnRunForm"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content">
   </Button>
</LinearLayout>

</LinearLayout>




需要提醒的是,要修改布局,可以直接编辑 XML,也可以使用 Android Developer Tools 中包含的 Layout 工具(见  所示)。
图 7. Layout 工具要运行这个应用程序,单击主屏幕图标,启动 XmlGui Activity(如  所示)。
图 8. 运行中的应用程序
返回列表