.Net程序员安卓学习之路1:登陆界面

简介: 原文:.Net程序员安卓学习之路1:登陆界面  任何编程学习起步均是HelloWorld,作为稍有>net编程经验的我们来说就跳过这步吧,咱们且从简单登录界面开始。先看看效果: 一、准备知识: 1. 安卓环境:安装好JDK,直接去官网下载ADT-bundle集成包后更新即可使用。
原文: .Net程序员安卓学习之路1:登陆界面

 

任何编程学习起步均是HelloWorld,作为稍有>net编程经验的我们来说就跳过这步吧,咱们且从简单登录界面开始。先看看效果:

wpsC63F.tmp

一、准备知识:

1. 安卓环境:安装好JDK,直接去官网下载ADT-bundle集成包后更新即可使用。

2. 项目目录:一张图说明一切
wpsC650.tmp

二、页面布局:

还是一幅图说明一切

wpsC651.tmp

那么这个界面的布局如何呢?

<LinearLayout >最外边的DIV,用的是线性布局,方向是垂直

    <TextView/>就是上图“初始用户名。。。”这几个字所在Lable

    <LinearLayout >里面的DIV,水平布局
        <TextView/>用户名Lable
        <EditText/>用户名TextBox
    </LinearLayout>

    <LinearLayout>里面的DIV,水平布局
        <TextView/>密码Lable
        <EditText/>密码TextBox
    </LinearLayout>

    <Button/>登录按钮

</LinearLayout>

上图一看,就会一半,下来一个一个看:

 

1. 最外层DIV:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" android:layout_margin="5dip" >

定义宽高的停靠方式,有

fill_parent、match_parent:是一样的,为了兼容低版本,建议使用fill_parent

设置布局/控件为fill_parent将强制性让它布满整个屏幕或填满父控件的空白

wrap_content:被内容撑大,刚好能显示下内容为止

Orientation:排列方式,vertical垂直,默认是HORIZONTAL水平

 

2. 文本框:

<TextView
android:id="@+id/lbl_LoginPass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/lbl_LoginPass_Text"
android:textSize="15.0sp" />

这里出现了两个未知属性:

@+id/lbl_LoginPass和@string/lbl_LoginPass_Text

其实完全可以直接写:

android:text="用户名"

系统会提示这是硬编码,建议改写(汗,写了这么多年硬编码),他意思是这里仅仅引用字典中一个变量的名称,具体的值在字典中去维护,那么字典在哪里呢?

Res/Values/String.xml中维护了这个字典:

<resources>
    <string name="app_name">登录DEMO</string>
    <string name="action_settings">Settings</string>
    <string name="lbl_LoginName">用户名:</string>
    <string name="lbl_LoginPass_Text">密    码:</string>
    <string name="btn_login">开始登陆</string>
</resources>

这里编码规范得注意一下了:假如控件ID为lbl_LoginPass,则他的字典名应该为lbl_LoginPass_Text为了防止多个页面的字典名重复建议最好加上页面前缀,如Login_lbl_LoginPass_Text

完整代码如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" android:layout_margin="5dip" >
            <TextView
                android:id="@+id/form_title"
                android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                android:text="初始用户名和密码都是123" />
        <LinearLayout
        android:id="@+id/layout_login_name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5.0dip"
        android:layout_marginTop="10.0dip"
        android:orientation="horizontal" >
            <TextView
                android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                android:text="@string/lbl_LoginName" />
            <EditText
                android:id="@+id/txt_login_name"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:textSize="15.0sp" />
        </LinearLayout>

        
        <LinearLayout
        android:id="@+id/login_pwd_layout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/layout_login_name"
        android:layout_centerHorizontal="true"
        android:layout_margin="5.0dip"
        android:orientation="horizontal" >
            <TextView
                android:id="@+id/login_pass_edit"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/lbl_LoginPass"
                android:textSize="15.0sp" />

            <EditText
                android:id="@+id/txt_login_pwd"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:password="true"
                android:textSize="15.0sp" />
            </LinearLayout>
            
           <Button
        android:id="@+id/btn_login"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:onClick="btn_click"
        android:text="登陆" />
</LinearLayout>

3. 页面后台
打开页面对应的后台代码:MainActivity.java

手动实现按钮的点击事件:

    public void btn_click(View v)
    {
        TextView lblInfo=(TextView)findViewById(R.id.form_title);
        
        EditText txt_login_name=(EditText)findViewById(R.id.txt_login_name);
        EditText txt_login_pass=(EditText)findViewById(R.id.txt_login_pwd);
        String loginName=txt_login_name.getText().toString().trim();
        String loginPass=txt_login_pass.getText().toString().trim();
        if(loginPass.equals("123")&&loginName.equals("123"))
        {
            lblInfo.setText("登录成功!");
        }
        else
        {
            lblInfo.setText("登录失败!");
        }
        
    }

其实这些倒没啥说的一看名字就知道啥意思。

wpsC661.tmp

wpsC662.tmp

目录
相关文章
|
5月前
|
存储 消息中间件 人工智能
【03】AI辅助编程完整的安卓二次商业实战-本地构建运行并且调试-二次开发改注册登陆按钮颜色以及整体资源结构熟悉-优雅草伊凡
【03】AI辅助编程完整的安卓二次商业实战-本地构建运行并且调试-二次开发改注册登陆按钮颜色以及整体资源结构熟悉-优雅草伊凡
219 3
|
4月前
|
人工智能 API 数据库
Semantic Kernel .NET 架构学习指南
本指南系统解析微软Semantic Kernel .NET架构,涵盖核心组件、设计模式与源码结构,结合实战路径与调试技巧,助你从入门到贡献开源,掌握AI编排开发全栈技能。
446 2
|
Java 物联网 C#
C#/.NET/.NET Core学习路线集合,学习不迷路!
C#/.NET/.NET Core学习路线集合,学习不迷路!
606 0
|
Web App开发 安全 程序员
FFmpeg开发笔记(五十五)寒冬里的安卓程序员可进阶修炼的几种姿势
多年的互联网寒冬在今年尤为凛冽,坚守安卓开发愈发不易。面对是否转行或学习新技术的迷茫,安卓程序员可从三个方向进阶:1)钻研谷歌新技术,如Kotlin、Flutter、Jetpack等;2)拓展新功能应用,掌握Socket、OpenGL、WebRTC等专业领域技能;3)结合其他行业,如汽车、游戏、安全等,拓宽职业道路。这三个方向各有学习难度和保饭碗指数,助你在安卓开发领域持续成长。
320 1
FFmpeg开发笔记(五十五)寒冬里的安卓程序员可进阶修炼的几种姿势
|
Java Maven 开发工具
第一个安卓项目 | 中国象棋demo学习
本文是作者关于其第一个安卓项目——中国象棋demo的学习记录,展示了demo的运行结果、爬坑记录以及参考资料,包括解决Android Studio和maven相关问题的方法。
274 7
第一个安卓项目 | 中国象棋demo学习
|
存储 开发工具 Android开发
使用.NET MAUI开发第一个安卓APP
【9月更文挑战第24天】使用.NET MAUI开发首个安卓APP需完成以下步骤:首先,安装Visual Studio 2022并勾选“.NET Multi-platform App UI development”工作负载;接着,安装Android SDK。然后,创建新项目时选择“.NET Multi-platform App (MAUI)”模板,并仅针对Android平台进行配置。了解项目结构,包括`.csproj`配置文件、`Properties`配置文件夹、平台特定代码及共享代码等。
1381 2
|
开发框架 缓存 算法
开源且实用的C#/.NET编程技巧练习宝库(学习,工作,实践干货)
开源且实用的C#/.NET编程技巧练习宝库(学习,工作,实践干货)
854 0
|
Web App开发 编解码 视频直播
视频直播技术干货(十二):从入门到放弃,快速学习Android端直播技术
本文详细介绍了Android端直播技术的全貌,涵盖了从实时音视频采集、编码、传输到解码与播放的各个环节。文章还探讨了直播中音视频同步、编解码器选择、传输协议以及直播延迟优化等关键问题。希望本文能为你提供有关Andriod端直播技术的深入理解和实践指导。
646 0
|
Android开发
Android中关于登陆拦截逻辑流程(Intent传递使用步骤)
Android中关于登陆拦截逻辑流程(Intent传递使用步骤) 问题: 我们在开发中经常遇到这样的需求,当前用户在A界面点击按钮想跳到C界面,但是该用户当前并未登陆账户,此时我们就想让用户在跳到C界面之前跳到B界面进行登陆操作,当登陆成功时候直接跳到C界面,整个思路流程如下图所示; 解决方式.
2477 0