Android应用开发——界面设计

工程代码下载地址

一、设计目的

  1. 掌握常用组件在布局文件中的设置
  2. 掌握在java程序中获取组件值
  3. 掌握对组件值得验证
  4. 掌握基本常用的监听器,和事件处理
  5. 掌握将组件值提交到下一个Activity活动的方法

二、设计内容

完成注册信息界面,部门列表框,单击确定检查提交成功、接受界面

三、软硬件环境

开发环境:Android Studio
模拟运行:Android Emulator – Nexus_5X_API_24

四、实现过程及结果

4.1 设计UI界面

4.1.1 将主布局修改为线性布局LinearLayout,垂直排列

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>

4.1.2 在主布局中添加用户名文本框和输入框,添加密码文本框和输入框

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="用户名!"
        />
    <EditText
        android:id="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="密码"/>
    <EditText
        android:id="@+id/password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword"/>

4.1.3 在主布局中添加性别文本框和复选框

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="性别"/>
<RadioGroup
        android:id="@+id/sex"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/man"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="男"/>
        <RadioButton
            android:id="@+id/woman"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="女"/>
    </RadioGroup>

4.1.4 在主布局中添加联系电话文本框和输入框,设置输入类型为android:inputType=”text|phone”

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="联系电话"/>
<EditText
        android:id="@+id/tel"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="text|phone"/>

4.1.5 在主布局中添加部门文本框和列表框,设置列表框数据来源为depts.xml

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="部门"/>
<Spinner
        android:id="@+id/dept"
        android:entries="@array/dept"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"></Spinner>

4.1.6 在主布局中添加爱好文本框和一个线性布局,该布局中放置四个CheckBox组件,分别表示书籍、运动、音乐和电影

<TextView
        android:text="爱好"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <CheckBox
            android:id="@+id/book"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="书籍"/>
        <CheckBox
            android:id="@+id/sport"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="运动"/>
        <CheckBox
            android:id="@+id/music"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="音乐"/>
        <CheckBox
            android:id="@+id/movie"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
android:text="电影"/>
                    </LinearLayout>

4.1.7 在主布局中最后添加一个确定按钮,设置android:onClick属性值为myclink,即当单机确定按钮时会调用myclick方法

<Button
        android:id="@+id/ok"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="确定"
        android:onClick="myclick"/>

4.1.8完成后的界面如图所示:

这里写图片描述

4.2 表示部门的Spinner组件,其数据来源文件depts.xml位于res/values目录下

depts.xml:
    <?xml version="1.0" encoding="utf-8"?>
<resources>
        <string-array name="dept">
            <item>人力资源部</item>
            <item>销售部</item>
            <item>财务部</item>
            <item>开发部</item>
        </string-array>
</resources>

4.3 设计后台程序

4.3.1 在主Activity文件中,定义布局中的各组件对象和一个存放爱好中各复选框对象的favs动态数组

private ArrayList<CheckBox> favs;
private EditText userName;
private EditText password;
private EditText telephone;
private Spinner dept;
private RadioGroup sex;
private RadioButton man;
private RadioButton woman;

4.3.2 在onCreate()方法中,获取各组件

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        userName=(EditText)findViewById(R.id.name);
        password=(EditText)findViewById(R.id.password);
        telephone=(EditText)findViewById(R.id.tel);
        sex=(RadioGroup)findViewById(R.id.sex);
        dept=(Spinner)findViewById(R.id.dept);
         man=(RadioButton)findViewById(R.id.man);
        woman=(RadioButton)findViewById(R.id.woman);
        favs=new ArrayList<CheckBox>();
        CheckBox book=(CheckBox) findViewById(R.id.book);
        CheckBox sport=(CheckBox) findViewById(R.id.sport);
        CheckBox music=(CheckBox) findViewById(R.id.music);
        CheckBox movie=(CheckBox) findViewById(R.id.movie);
        favs.add(book);
        favs.add(sport);
        favs.add(music);
        favs.add(movie);
    }

4.3.3 获取性别方法

public String getSex(){
RadioButtonradioButton= (RadioButton)findViewById(sex.getCheckedRadioButtonId());
    return radioButton.getText().toString();
    }

4.3.4 获取爱好方法,爱好以逗号分隔

public String getFavorite()
                {
                String favo="";
                for(CheckBox cb:favs){
                    if(cb.isChecked()){
                        favo+=cb.getText().toString();
                        favo+=",";
                    }
                }
                if(!"".equals(favo))
                    favo=favo.substring(0,favo.length()-1);
                else
                    favo="您未选择爱好!";
                return favo;
                }

4.3.5 当检查通过时,输出注册信息,提交到下一个Activity页面

public void myclick(View view){
        //if (true){
            StringBuilder sb =new StringBuilder();
            sb.append("用户名: "+userName.getText().toString()+"\n");
        Log.v("name","done");
            sb.append("性别: "+getSex()+"\n");
        Log.v("sex","done");
            sb.append("电话: "+telephone.getText().toString()+"\n");
        Log.v("tel","done");
            sb.append("部门: "+dept.getSelectedItem().toString()+"\n");
        Log.v("part","done");
            sb.append("爱好: "+getFavorite()+"\n");
        Log.v("fav","done");
            Toast.makeText(this,sb.toString(),Toast.LENGTH_LONG).show();
        Intent intent = new Intent();
        intent.setClass(this,ResultActivity.class);
        intent.putExtra("info",sb.toString());
        this.startActivity(intent);
        //}
    }

4.3.6 创建一个result_activity.xml布局文件,放置一个文本框组件,并创建ResultActivity类,修改onCreate()方法,显示上页传来的数据

result_activity.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.zkj.test2.ResultActivity">

    <TextView
        android:id="@+id/result"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</android.support.constraint.ConstraintLayout>

        ResultActivity:
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_result);
        TextView result =(TextView)findViewById(R.id.result);
        result.setText("从前一页面传来如下:\n\n"+this.getIntent().getStringExtra("info"));

4.3.7 填写信系并点击确认,传递到下一页面:

这里写图片描述

  • 4
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 新闻app是一款基于Android平台的小型项目应用程序,它主要用于展示各类新闻内容,为用户提供便捷的阅读体验。该项目的源码包含了应用程序的基本框架和功能实现,方便开发者进行二次开发和定制。 新闻app的源码主要包含以下几个方面的内容: 1. 用户界面设计:源码中包含了新闻app的界面布局和样式,开发者可以根据自己的需要进行修改和美化。用户界面通常包括新闻列表、新闻详情页、分类标签等,开发者可以自由设计并添加其他功能模块。 2. 数据获取与展示:源码中实现了与服务器进行数据交互的功能,通过网络请求获取新闻数据,并在界面上展示出来。开发者可以根据需要修改数据请求接口和解析方式,实现与自己的服务器交互。 3. 新闻分类与搜索:源码中提供了新闻分类和搜索功能的实现,用户可以根据自己的兴趣和需求选择不同的新闻分类进行浏览,也可以通过搜索关键词进行精确定位。 4. 用户交互与分享:源码中包含了用户的登录注册功能和新闻内容的分享功能,用户可以通过登录账号进行个性化设置和收藏喜欢的新闻内容,也可以将新闻分享到社交媒体上与他人交流。 总之,新闻app源码是一个基础框架,开发者可以在此基础上进行二次开发和定制,根据自己的需求添加功能模块和美化界面,实现自己独特的新闻应用。 ### 回答2: Android新闻App是一个基于Android平台开发的小型项目,它的主要功能是提供最新的新闻内容给用户,并且用户可以进行浏览、搜索和分享等操作。下面是这个项目的一些关键特点和所需的源码组成部分: 1. 特点: - 用户界面友好,交互性强,提供舒适的浏览体验; - 支持实时更新,提供最新的新闻内容; - 具备搜索功能,方便用户查找感兴趣的新闻; - 支持新闻分享功能,方便用户将新闻分享给朋友; - 具备图文混排的能力,可以展示新闻的文字和图片。 2. 源码组成部分: - 主界面布局代码:定义了App的整体布局结构,包括顶部导航栏、底部工具栏和新闻显示区域等。 - 数据源代码:负责获取新闻数据,可以通过API接口获取最新的新闻内容,也可以从本地数据库获取已缓存的新闻数据。 - 新闻列表适配器代码:用于将新闻数据展示在界面上,包括标题、描述和图片等。 - 新闻详情界面代码:用于显示单篇新闻的详细内容,包括标题、正文和相关图片等。 - 搜索功能代码:实现了按关键字搜索新闻的功能,可以在已有的新闻数据中进行筛选。 - 分享功能代码:集成了社交媒体的分享SDK,方便用户将新闻内容分享给朋友。 - 图片加载和缓存代码:处理了新闻中的图片加载和本地缓存,提高了图片加载速度和用户体验。 通过以上的源码组成部分,可以完成一个基本的新闻App,用户可以在界面上浏览最新的新闻内容,进行搜索和分享操作。这个小项目可以帮助开发者理解Android开发框架和开发方式,提高编码能力和UI设计能力。 ### 回答3: 新闻app是基于Android平台开发的一个小型应用程序,可以提供用户各种最新的新闻资讯。以下是关于这个项目的源码介绍。 该项目源码主要由Java语言编写,使用了Android Studio作为开发工具。代码结构清晰,包含了主要的几个模块。 1. 用户界面模块:这个模块负责显示新闻列表和新闻详情等信息,主要包含布局文件和相应的逻辑代码。列表界面使用RecyclerView控件展示新闻列表,详情界面使用WebView展示新闻内容。 2. 网络请求模块:这个模块负责与后台服务器进行数据交互,使用了Android的HttpURLConnection类来发送请求和接收响应。请求参数可以根据实际需要进行修改,例如可以根据新闻类别进行请求。 3. 数据解析模块:这个模块负责解析从服务器返回的JSON格式的数据,转换成Java对象供应用程序使用。可以使用Android提供的JSON解析库,如Gson。 4. 数据存储模块:这个模块负责缓存新闻数据,以提高应用程序的响应速度。可以使用SharedPreferences或SQLite数据库来存储新闻数据。同时也可以使用图片缓存库,如Glide或Picasso来缓存新闻图片。 5. 用户交互模块:这个模块负责处理用户的交互行为,例如点击新闻列表项跳转到新闻详情界面,下拉刷新获取最新数据等。可以使用Android提供的相关控件和事件监听器来实现用户交互。 除了以上几个主要的模块,还可以根据需要添加其他功能,例如搜索栏、分享按钮等。 总体来说,这个新闻app的源码提供了一个完整的开发框架,初学者可以通过阅读和理解源码来学习Android应用程序的开发流程和一些常用技术。同时,也可以根据实际需求进行二次开发,添加新的功能和改进用户体验。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值