android app开发学习之表单组件学习(一)

这阵子不知道怎么了,心态不稳很着急。恰好前几天看到一句话分享给大家

我最近处理问题有点急,这是老年人心态

以上为邓爷爷在1993年发出的感叹。想来心态平稳才是年轻人应该有的心态。

这几天继续学习andorid app开发,学习了表单组件部分,记录一下学习过程。最终layout代码

xml 复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent">
    <Button
        android:id="@+id/reset_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/email"
        android:layout_marginTop="16dp"
        android:text="Reset Password"
        android:background="@drawable/button_background"/> <!-- 使用自定义的背景 -->
    <RadioGroup
        android:id="@+id/radioGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <RadioButton
            android:id="@+id/radioButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Option 1" />

        <RadioButton
            android:id="@+id/radioButton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Option 2" />

    </RadioGroup>
    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Option 1" />

    <CheckBox
        android:id="@+id/checkBox2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Option 2" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="输入:" />

        <EditText
            android:id="@+id/editText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Enter text here" />

    </LinearLayout>
</LinearLayout>

以上最外层是一个LinearLayout线性布局标签,里面写了按钮Button、单选框组RadioGroup、复选框CheckBox和文本输入框。

我首先是新创建了一个layout布局文件activity_form,然后添加一个class名称ActivityForm,然后将这个类添加到AndroidManifest.xml文件中,也就是相当于vue中的注册一下路由,以便可以访问到。然后就开始开发了。

Button

xml 复制代码
 <Button
        android:id="@+id/reset_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/email"
        android:layout_marginTop="16dp"
        android:text="Reset Password"
        android:background="@drawable/button_background"/> <!-- 使用自定义的背景 -->

Button组件这里使用android:background="@drawable/button_background"作为背景图,@drawable/button_background具体代码

xml 复制代码
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#3F51B5"/> <!-- 按钮的背景颜色 -->

    <corners android:radius="20dp"/> <!-- 按钮的圆角半径 -->

    <stroke android:color="#FFFFFF" android:width="2dp"/> <!-- 按钮的边框颜色和宽度 -->

    <padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp"/> <!-- 按钮的内边距 -->
</shape>

具体上面是什么含义有注释。包括背景的颜色,按钮的圆角半径、按钮的边框颜色和宽度、按钮的内边距。 这个竟然可以是单独拿出来的,相对web开发,不借助库的话,原生还是不好这样实现的,颗粒度上android要比web更细。

RadioGroup

xml 复制代码
  <RadioGroup
        android:id="@+id/radioGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <RadioButton
            android:id="@+id/radioButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Option 1" />

        <RadioButton
            android:id="@+id/radioButton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Option 2" />

    </RadioGroup>

就是一个单选框组,之后相应的java脚本如下

java 复制代码
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.radioButton1:
                // handle radioButton1
                break;
            case R.id.radioButton2:
                // handle radioButton2
                break;
        }
    }
});

CheckBox

xml 复制代码
 <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Option 1" />

    <CheckBox
        android:id="@+id/checkBox2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Option 2" />

复选框对应的java脚本

java 复制代码
CheckBox checkBox1 = (CheckBox) findViewById(R.id.checkBox1);
checkBox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            // handle checkBox1 is checked
        } else {
            // handle checkBox1 is not checked
        }
    }
});
CheckBox checkBox2 = (CheckBox) findViewById(R.id.checkBox2);
checkBox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            // handle checkBox2 is checked
        } else {
            // handle checkBox2 is not checked
        }
    }
});

EditText

xml 复制代码
 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="输入:" />

        <EditText
            android:id="@+id/editText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Enter text here" />

    </LinearLayout>

EditText文本输入框使用了线性布局的水平布局。给输入框安排了一个label。

总结

整体来说,本次学习的比较简单。开发体验上的话,有点类似vue开发的感觉,添加一个路由,然后在开发页面。

对于android开发,开发一个activity,包括它的class和layout,之后把class注册到配置文件AndroidManifest.xml,之后就是具体的功能开发:包括布局和脚本两部分。

本文完。

相关推荐
2501_915909062 小时前
Charles中文版使用教程 高效抓包与API调试常见问题处理
android·ios·小程序·https·uni-app·iphone·webview
智江鹏2 小时前
Android 之 RxJava2
android
御水流红叶3 小时前
安卓加固脱壳
android·开发语言·python
智江鹏4 小时前
Android 之 网络通信(HTTP/TCP/UDP/JSON)
android
xq95274 小时前
android webview和 js 各种用法交互
android
北有花开6 小时前
Android方法耗时监控插件:基于ASM字节码插桩的性能分析工具
android
whysqwhw6 小时前
React Native应用中实现原生组件与JavaScript组件的复杂交互
android
whysqwhw6 小时前
React Native 中调用 Android 自定义控件
android
往事如烟隔多年6 小时前
一加Ace5无法连接ColorOS助手解决(安卓设备ADB模式无法连接)
android·adb·手机·coloros
00后程序员张6 小时前
iOS软件性能监控实战指南 开发到上线的完整流程解析
android·ios·小程序·https·uni-app·iphone·webview