今天主要使用的布局:LinerLayout
使用的控件有:TextView ListView EditView RadioGroup RadioButton Button Spinner
File -> New ->Activity ->Empty Activity
这样就能直接生成文件:
第二步.点击res -> layout ->【activity_list.xml】
分析页面元素:发现是有7个重复的板块,每个版本都是由4部分组成
<style name="list_text_one">
- "android:layout_width"
>0dp
- "android:layout_height">match_parent
- "android:gravity">center
- "android:textSize">50sp
- "android:textColor">@color/black
style>
【activity_list.xml】
<LinearLayout 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"
android:orientation="vertical"
tools:context=".ListActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:background="@drawable/custom_border_">
<TextView
android:layout_weight="3"
android:text="5"
style="@style/list_text_one"/>
<LinearLayout
android:layout_weight="7"
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp">
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="2014-02-19 17:50:35"
android:textColor="@color/black"
android:textSize="15sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="Admin"
android:textColor="@color/black"
android:textSize="15sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="2014-2-19"
android:textColor="@color/black"
android:textSize="15sp"/>
LinearLayout>
<TextView
android:id="@+id/tvAudit"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="未审"
android:gravity="center_vertical |right"
android:textSize="16sp"/>
<Button
android:id="@+id/btnAudit"
android:layout_weight="3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:text="审核"
android:gravity="center"
android:textSize="22sp"/>
LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:background="@drawable/custom_border_">
<TextView
android:layout_weight="3"
android:text="6"
style="@style/list_text_one"/>
<LinearLayout
android:layout_weight="7"
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp">
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="2014-02-19 17:50:35"
android:textColor="@color/black"
android:textSize="15sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="Admin"
android:textColor="@color/black"
android:textSize="15sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="2014-2-19"
android:textColor="@color/black"
android:textSize="15sp"/>
LinearLayout>
<TextView
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="未审"
android:gravity="center_vertical |right"
android:textSize="16sp"/>
<Button
android:layout_weight="3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:text="审核"
android:gravity="center"
android:textSize="22sp"/>
LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:background="@drawable/custom_border_">
<TextView
android:layout_weight="3"
android:text="6"
style="@style/list_text_one"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="7"
android:paddingTop="5dp"
android:lines="3"
android:text="2014-02-19 17:50:35\nAdmin\n2014-02-19"
android:textColor="@color/black"
android:textSize="15sp"
android:lineSpacingMultiplier="2"/>
<TextView
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="未审"
android:gravity="center_vertical |right"
android:textSize="16sp"/>
<Button
android:layout_weight="3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:text="审核"
android:gravity="center"
android:textSize="22sp"/>
LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:background="@drawable/custom_border_">
<TextView
android:layout_weight="3"
android:text="8"
style="@style/list_text_one"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="7"
android:paddingTop="5dp"
android:lines="3"
android:text="2014-02-19 17:50:35\nAdmin\n2014-02-19"
android:textColor="@color/black"
android:textSize="15sp"
android:lineSpacingMultiplier="2"/>
<TextView
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="未审"
android:gravity="center_vertical |right"
android:textSize="16sp"/>
<Button
android:layout_weight="3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:text="审核"
android:gravity="center"
android:textSize="22sp"/>
LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:background="@drawable/custom_border_">
<TextView
android:layout_weight="3"
android:text="9"
style="@style/list_text_one"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="7"
android:paddingTop="5dp"
android:lines="3"
android:text="2014-02-19 17:50:35\nAdmin\n2014-02-19"
android:textColor="@color/black"
android:textSize="15sp"
android:lineSpacingMultiplier="2"/>
<TextView
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="未审"
android:gravity="center_vertical |right"
android:textSize="16sp"/>
<Button
android:layout_weight="3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:text="审核"
android:gravity="center"
android:textSize="22sp"/>
LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:background="@drawable/custom_border_">
<TextView
android:layout_weight="3"
android:text="10"
style="@style/list_text_one"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="7"
android:paddingTop="5dp"
android:lines="3"
android:text="2014-02-19 17:50:35\nAdmin\n2014-02-19"
android:textColor="@color/black"
android:textSize="15sp"
android:lineSpacingMultiplier="2"/>
<TextView
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="未审"
android:gravity="center_vertical |right"
android:textSize="16sp"/>
<Button
android:layout_weight="3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:text="审核"
android:gravity="center"
android:textSize="22sp"/>
LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:background="@drawable/custom_border_">
<TextView
android:layout_weight="3"
android:text="11"
style="@style/list_text_one"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="7"
android:paddingTop="5dp"
android:lines="3"
android:text="2014-02-19 17:50:35\nAdmin\n2014-02-19"
android:textColor="@color/black"
android:textSize="15sp"
android:lineSpacingMultiplier="2"/>
<TextView
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="未审"
android:gravity="center_vertical |right"
android:textSize="16sp"/>
<Button
android:layout_weight="3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:text="审核"
android:gravity="center"
android:textSize="22sp"/>
LinearLayout>
LinearLayout>
【ListActivity.java】
package com.example.firemanager;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
public class ListActivity extends AppCompatActivity {
private Button btnAudit;
private TextView tvAudit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
lvDatetime = findViewById(R.id.lvDatetime);
btnAudit = findViewById(R.id.btnAudit);
tvAudit = findViewById(R.id.tvAudit);
btnAudit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tvAudit.setText("已审");
}
});
}
}
最后查看【manifests】
<activity android:name=".ListActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
intent-filter>
activity>
【string.xml】
<resources>
<string name="app_name">FireManagerstring>
<string name="time">交付时间:string>
<string name="number">数量:string>
<string name="engine">发动机排量:string>
<string name="gearbox">变速箱:string>
<string name="wheel">轮毂:string>
<string name="central">中控:string>
<string name="brake">刹车:string>
<string name="hang">悬挂:string>
<string-array name="hang_list">
<item>独立悬挂系统item>
<item>主动悬挂系统item>
<item>横臂式悬挂系统item>
<item>纵臂式悬挂系统item>
<item>烛式悬挂系统item>
<item>多连杆式悬挂系统item>
<item>麦弗逊式悬挂系统item>
string-array>
<string-array name="emissions_list">
<item>1.0item>
<item>2.0item>
<item>3.0item>
<item>4.0item>
string-array>
<string-array name="datetime_info">
<item>2014-02-19 17:50:35item>
<item>Adminitem>
<item>2014-02-19item>
string-array>
resources>
【activity_form.xml】
<LinearLayout 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"
android:background="@color/basic"
android:orientation="vertical"
android:padding="4dp"
tools:context=".FormActivity">
<LinearLayout
style="@style/form_liner"
android:layout_weight="1"
>
<TextView
android:id="@+id/tvTime"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:text="@string/time"
android:textColor="@color/black"
android:textSize="15dp"/>
LinearLayout>
<LinearLayout
style="@style/form_liner"
android:layout_weight="1">
<TextView
android:id="@+id/tvNum"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:text="@string/number"
android:textColor="@color/black"
android:textSize="15dp"/>
<EditText
android:id="@+id/edNum"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:background="@drawable/edit_background"
android:lines="1"/>
LinearLayout>
<LinearLayout
style="@style/form_liner"
android:layout_weight="1"
>
<TextView
android:id="@+id/tvEngine"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:text="@string/engine"
android:textColor="@color/black"
android:textSize="15dp"/>
<Spinner
android:layout_width="120dp"
android:layout_height="wrap_content"
android:background="@drawable/edit_background"
android:entries="@array/emissions_list"/>
LinearLayout>
<LinearLayout
style="@style/form_liner"
android:layout_weight="1"
>
<TextView
android:id="@+id/tvGearbox"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:text="@string/gearbox"
android:textColor="@color/black"
android:textSize="15dp"/>
<RadioGroup
android:id="@+id/rgGearbox"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="center">
<RadioButton
android:id="@+id/rbCearbox_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="自动"
android:checked="true"/>
<RadioButton
android:id="@+id/rbCearbox_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="手动"/>
RadioGroup>
LinearLayout>
<LinearLayout
style="@style/form_liner"
android:layout_weight="1">
<TextView
android:id="@+id/tvWheel"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:text="@string/wheel"
android:textColor="@color/black"
android:textSize="15dp"/>
<RadioGroup
android:id="@+id/rgWheel"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="center">
<RadioButton
android:id="@+id/rbWheel_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="烤漆"
android:checked="true"/>
<RadioButton
android:id="@+id/rbWheel_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="电镀"/>
RadioGroup>
LinearLayout>
<LinearLayout
style="@style/form_liner"
android:layout_weight="1"
>
<TextView
android:id="@+id/tvCentral"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:text="@string/central"
android:textColor="@color/black"
android:textSize="15dp"/>
<RadioGroup
android:id="@+id/rgCentral"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="center">
<RadioButton
android:id="@+id/rbCentral_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="低配"
android:checked="true"/>
<RadioButton
android:id="@+id/rbCentral_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="高配"/>
RadioGroup>
LinearLayout>
<LinearLayout
style="@style/form_liner"
android:layout_weight="1"
>
<TextView
android:id="@+id/tvBreak"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:text="@string/brake"
android:textColor="@color/black" />
<RadioGroup
android:id="@+id/rgBreak"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="center">
<RadioButton
android:id="@+id/rbBreak_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="鼓式制动器"
android:checked="true"/>
<RadioButton
android:id="@+id/rbBreak_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="盘式制动器"/>
RadioGroup>
LinearLayout>
<LinearLayout
style="@style/form_liner"
android:layout_weight="1"
>
<TextView
android:id="@+id/tvHang"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:text="@string/hang"
android:textColor="@color/black"
android:textSize="15dp"/>
<Spinner
android:id="@+id/spHang"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/edit_background"
android:entries="@array/hang_list"/>
LinearLayout>
<LinearLayout
style="@style/form_liner"
android:layout_weight="2">
LinearLayout>
<LinearLayout
style="@style/form_liner"
android:gravity="center_horizontal"
android:layout_margin="0dp"
android:layout_weight="2">
<Button
android:layout_width="100dp"
android:layout_height="80dp"
android:text="确定"
android:background="@drawable/custom_border_"/>
LinearLayout>
LinearLayout>
【style.xml】
<style name="form_liner">
- "android:layout_width"
>match_parent
- "android:layout_height">0dp
- "android:background">@color/white
- "android:layout_margin">2dp
- "android:gravity">center_vertical
- "android:padding">10dp
style>
【ListActivity.java】
package com.example.firemanager;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.DatePicker;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.NumberPicker;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TimePicker;
import java.util.ArrayList;
import java.util.List;
public class FormActivity extends AppCompatActivity {
//变速箱
private RadioGroup rgGearbox;
private RadioButton rbGearbox_1;
private RadioButton rbGearbox_2;
//轮毂
private RadioGroup rgWheel;
private RadioButton rbWheel_1;
private RadioButton rbWheel_2;
//中控
private RadioGroup rgCentral;
private RadioButton rbCentral_1;
private RadioButton rbCentral_2;
//刹车
private RadioGroup rgBreak;
private RadioButton rbBreak_1;
private RadioButton rbBreak_2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_form);
rgGearbox = findViewById(R.id.rgGearbox);
rbGearbox_1 = findViewById(R.id.rbCearbox_1);
rbGearbox_2 = findViewById(R.id.rbCearbox_2);
rgWheel = findViewById(R.id.rgWheel);
rbWheel_1= findViewById(R.id.rbWheel_1);
rbWheel_2 = findViewById(R.id.rbWheel_2);
rgCentral = findViewById(R.id.rgCentral);
rbCentral_1 = findViewById(R.id.rbCentral_1);
rbCentral_2 = findViewById(R.id.rbCentral_2);
rgBreak = findViewById(R.id.rgBreak);
rbBreak_1 = findViewById(R.id.rbBreak_1);
rbBreak_2 = findViewById(R.id.rbBreak_2);
rgGearbox.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(rbGearbox_1.isChecked()) {
rbGearbox_2.setChecked(false);
} else {
rbGearbox_1.setChecked(false);
}
}
});
rgWheel.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(rbWheel_1.isChecked()) {
rbWheel_2.setChecked(false);
} else {
rbWheel_1.setChecked(false);
}
}
});
rgCentral.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(rbCentral_1.isChecked()) {
rbCentral_2.setChecked(false);
} else {
rbCentral_1.setChecked(false);
}
}
});
rgBreak.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(rbBreak_1.isChecked()) {
rbBreak_2.setChecked(false);
} else {
rbBreak_1.setChecked(false);
}
}
});
}
}
【Manifests】
<activity android:name=".FormActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
intent-filter>
activity>
问题:日期选择未能完成
总结:今天的练习使我插件的使用了解更加深刻,在写代码的过程中写了,会有很多重复的布局,昨天其实也发现了这问题,我看到别人的代码把一些重复的都写到style.xml里面来调用,今天我也尝试的做了,着实能使代码更简洁,我还发现xml里面的单选框并不能直接实现单选的效果,百度查看发现还需要在java文件里编写事件。