实验二:UI设计

实验目的

本次实验的目的是让大家熟悉Android开发中的UI设计,包括了解和熟悉常用控件的使用、界面布局和事件处理等内容。

实验要求

  1. 熟悉和掌握界面控件设计
  2. 了解Android界面布局
  3. 掌握控件的事件处理

实验内容

一、常用控件

1.常用控件介绍

Android中有许多常用控件(简单分类):

文本框:TextView、EditText

按钮:Button、RadioButton、RadioGroup、CheckBox、ImageButton、Switch

列表:List、ExpandableListView、Spinner、AutoCompleteTextView、GridView、ImageView

进度条:ProgressBar、ProgressDialog、SeekBar、RatingBar

选择器:DatePicker、TimePicker

菜单:Menu、ContentMenu

对话框:Dialog、ProgressDialog

图片显示:ImageViewImageButton

常用的控件有文本框、按钮和列表等。

2.控件的实现

在上一个实验中,我们用TextView实现了在界面显示一句Hello World!”。

public class hello extends Activity {
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       TextView textView = new TextView(this);
       textView.setText("Hello World!");
       setContentView(textView);
   }
}

 

现在我们来看一看.xml文件和编码的相互关系。

Hello World.java中敲入代码:

public class HelloWorld extends Activity {

       @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        TextView textView = (TextView) findViewById(R.id.textView01);

        // 获取 Button 控件

        Button button = (Button) findViewById(R.id.submitButton);

        button.setOnClickListener(v -> {

            textView.setText("Button clicked!");         });

    }

}

通过修改.xml文件来呈现界面,以及增加文本框和按钮等。在项目目录中找到/res/layout/main.xml,如下

最开始界面如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   >
   <TextView
       android:id="@+id/textView01"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:text="Hello World!"
       />
   </LinearLayout>

 

 

 

在编码中加入一个Button,在界面中显示一个按钮,

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

   android:orientation="vertical"

   android:layout_width="fill_parent"

   android:layout_height="fill_parent"

   >

   <TextView

       android:id="@+id/textView01"

       android:layout_width="fill_parent"

       android:layout_height="wrap_content"

       android:text="Hello World!"

       />

   <Button

       android:id="@+id/submitButton"

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:text="Submit"

       android:layout_gravity="center_horizontal" />

</LinearLayout>

在项目目录中找到/res/values/strings.xml,如下:

<resources>
   <string name="app_name">My Application5</string>
   <string name="hello">Hello World!</string>
   <string name="button">Submit</string>
</resources>

最后内容显示如下图:

 

 

posted @ 2025-03-24 09:22  不会JAVA的小袁  阅读(60)  评论(0)    收藏  举报