Android 车辆定制软件(车辆定制完成后将信息传至服务器)
第一步:在xml文件上界面布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<Button
android:id="@+id/b1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="40dp"
android:text="车辆定制"/>
<Button
android:id="@+id/b2"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="40dp"
android:text="订单管理"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="车型:"/>
<ImageView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:src="@mipmap/car1"/>
<ImageView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:src="@mipmap/car2"/>
<ImageView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:src="@mipmap/car3"/>
</LinearLayout>
<RadioGroup
android:id="@+id/r1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/r11"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="80dp"
android:checked="true"
android:text="轿车"
android:textColor="#FFFFFF"
android:layout_weight="1" />
<RadioButton
android:id="@+id/r12"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_marginLeft="40dp"
android:text="suv"
android:textColor="#FFFFFF"
android:layout_height="wrap_content" />
<RadioButton
android:id="@+id/r13"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_marginLeft="50dp"
android:text="mpv"
android:textColor="#FFFFFF"
android:layout_height="wrap_content" />
</RadioGroup>
<View
android:layout_width="match_parent"
android:background="#B6B4B4"
android:layout_marginTop="5dp"
android:layout_height="1dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="交付时间:"/>
<Spinner
android:id="@+id/s1"
android:layout_width="150dp"
android:layout_height="30dp"
android:layout_marginLeft="20dp"
android:entries="@array/two"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:background="#B6B4B4"
android:layout_marginTop="5dp"
android:layout_height="1dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="数量:"/>
<EditText
android:id="@+id/e1"
android:layout_width="130dp"
android:layout_height="30dp"
android:background="#FFFFFF"
android:layout_marginLeft="57dp"
android:singleLine="true"
android:inputType="number" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:background="#B6B4B4"
android:layout_marginTop="5dp"
android:layout_height="1dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textSize="12sp"
android:text="发动机排量:"/>
<Spinner
android:id="@+id/s2"
android:layout_width="150dp"
android:layout_height="30dp"
android:layout_marginLeft="20dp"
android:entries="@array/three"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:background="#B6B4B4"
android:layout_marginTop="5dp"
android:layout_height="1dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="变速箱:"/>
<RadioGroup
android:id="@+id/r2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="7dp"
android:orientation="horizontal">
<RadioButton
android:id="@+id/r21"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="自动"/>
<RadioButton
android:id="@+id/r22"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="手动"/>
</RadioGroup>
</LinearLayout>
<View
android:layout_width="match_parent"
android:background="#B6B4B4"
android:layout_marginTop="5dp"
android:layout_height="1dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="轮毂:"/>
<RadioGroup
android:id="@+id/r3"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="20dp"
android:orientation="horizontal">
<RadioButton
android:id="@+id/r31"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="烤漆"/>
<RadioButton
android:id="@+id/r32"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="电镀"/>
</RadioGroup>
</LinearLayout>
<View
android:layout_width="match_parent"
android:background="#B6B4B4"
android:layout_marginTop="5dp"
android:layout_height="1dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="中控:"/>
<RadioGroup
android:id="@+id/r4"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="20dp"
android:orientation="horizontal">
<RadioButton
android:id="@+id/r41"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="低配"/>
<RadioButton
android:id="@+id/r42"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="高配"/>
</RadioGroup>
</LinearLayout>
<View
android:layout_width="match_parent"
android:background="#B6B4B4"
android:layout_marginTop="5dp"
android:layout_height="1dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="刹车:"/>
<RadioGroup
android:id="@+id/r5"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="20dp"
android:orientation="horizontal">
<RadioButton
android:id="@+id/r51"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="鼓式制动器"/>
<RadioButton
android:id="@+id/r52"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="盘式制动器"/>
</RadioGroup>
</LinearLayout>
<View
android:layout_width="match_parent"
android:background="#B6B4B4"
android:layout_marginTop="5dp"
android:layout_height="1dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="悬挂:"/>
<Spinner
android:id="@+id/s3"
android:layout_width="200dp"
android:layout_marginLeft="20dp"
android:layout_height="wrap_content"
android:entries="@array/one" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:background="#B6B4B4"
android:layout_marginTop="5dp"
android:layout_height="1dp"/>
<Button
android:id="@+id/success"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_marginLeft="300dp"
android:text="下订单"/>
</LinearLayout>
使用Spinner控件时,需要先在res文件下的values创建array文件
输入以下代码来选择内容:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="one">
<item>2020/10/17</item>
<item>2020/10/18</item>
<item>2020/10/19</item>
<item>2020/10/20</item>
<item>2020/10/21</item>
<item>2020/10/22</item>
<item>2020/10/23</item>
</string-array>
<string-array name="two">
<item>1.0</item>
<item>2.0</item>
<item>3.0</item>
<item>4.0</item>
<item>5.0</item>
<item>6.0</item>
<item>7.0</item>
</string-array>
<string-array name="three">
<item>独立悬挂系统</item>
<item>主动悬挂系统</item>
<item>横臂式悬挂系统</item>
<item>纵臂式悬挂系统</item>
<item>烛式悬挂系统</item>
<item>多连杆式悬挂系统</item>
<item>麦弗逊式悬挂系统</item>
</string-array>
</resources>
界面截图:
第二步:在Mainactivity.java完成功能的实现
找到ID并绑定
实现功能
首先定义1个SharedPreferences和3个String类型的数组来存放数据
RadioGroup 控件1(r1)
r1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
RadioButton radioButton= (RadioButton)radioGroup.findViewById(i);//获取数据
sharedPreferences=getSharedPreferences("date", Context.MODE_PRIVATE);
SharedPreferences.Editor editor1=sharedPreferences.edit();
editor1.putString("one",radioButton.getText().toString());//保存数据
editor1.apply();
String a=sharedPreferences.getString("one","");//得到数据
Toast.makeText(MainActivity.this,a,Toast.LENGTH_SHORT).show();//显示数据
Toast.makeText(MainActivity.this,a,Toast.LENGTH_SHORT).show();
}
});
RadioGroup 控件2(r2)
r2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
RadioButton radioButton= (RadioButton)radioGroup.findViewById(i);//获取数据
sharedPreferences=getSharedPreferences("date", Context.MODE_PRIVATE);
SharedPreferences.Editor editor1=sharedPreferences.edit();
editor1.putString("two",radioButton.getText().toString());//保存数据
editor1.apply();
String a=sharedPreferences.getString("two","");//得到数据
Toast.makeText(MainActivity.this,a,Toast.LENGTH_SHORT).show();//显示数据
}
});
RadioGroup 控件3(r3)
r3.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
RadioButton radioButton= (RadioButton)radioGroup.findViewById(i);//获取数据
sharedPreferences=getSharedPreferences("date", Context.MODE_PRIVATE);
SharedPreferences.Editor editor1=sharedPreferences.edit();
editor1.putString("three",radioButton.getText().toString());//保存数据
editor1.apply();
String a=sharedPreferences.getString("three","");//得到数据
Toast.makeText(MainActivity.this,a,Toast.LENGTH_SHORT).show();//显示数据
}
});
RadioGroup 控件4(r4)
r4.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
RadioButton radioButton= (RadioButton)radioGroup.findViewById(i);//获取数据
sharedPreferences=getSharedPreferences("date", Context.MODE_PRIVATE);
SharedPreferences.Editor editor1=sharedPreferences.edit();
editor1.putString("four",radioButton.getText().toString());//保存数据
editor1.apply();
String a=sharedPreferences.getString("four","");//得到数据
Toast.makeText(MainActivity.this,a,Toast.LENGTH_SHORT).show();//显示数据
}
});
RadioGroup 控件5(r5)
r5.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
RadioButton radioButton= (RadioButton)radioGroup.findViewById(i);//获取数据
sharedPreferences=getSharedPreferences("date", Context.MODE_PRIVATE);
SharedPreferences.Editor editor1=sharedPreferences.edit();
editor1.putString("five",radioButton.getText().toString());//保存数据
editor1.apply();
String a=sharedPreferences.getString("five","");//得到数据
Toast.makeText(MainActivity.this,a,Toast.LENGTH_SHORT).show();//显示数据
}
});
- 3个Spinner的功能
首先选择Spinner的样式,在添加监听器
Spinner控件1(s1)
OneStr=getResources().getStringArray(R.array.one);
adapter1=new ArrayAdapter(this,android.R.layout.simple_spinner_item,OneStr);//将可选的内容与ArrayAdapter连接
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);//设置下拉列表的风格
s1.setAdapter(adapter1);
s1.setSelection(0,true);//默认选择的值
Spinner控件2(s2)
TwoStr=getResources().getStringArray(R.array.two);
adapter2=new ArrayAdapter(this,android.R.layout.simple_spinner_item,TwoStr);//将可选的内容与ArrayAdapter连接
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);//设置下拉列表的风格
s2.setAdapter(adapter2);
s2.setSelection(0,true);//默认选择的值
Spinner控件3(s3)
ThreeStr=getResources().getStringArray(R.array.three);
adapter3=new ArrayAdapter(this,android.R.layout.simple_spinner_item,ThreeStr);//将可选的内容与ArrayAdapter连接
adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);//设置下拉列表的风格
s3.setAdapter(adapter3);
s3.setSelection(0,true);//默认选择的值
Spinner控件监听器
在倒数第二个花括号定义3个类
private class SpinnerSelectedListener1 implements Spinner.OnItemSelectedListener
{
}
private class SpinnerSelectedListener2 implements Spinner.OnItemSelectedListener
{
}
private class SpinnerSelectedListener3 implements Spinner.OnItemSelectedListener
{
}
鼠标移至Spinner.OnItemSelectedListener,键盘按下Alt+Enter,选择第一个,点ok
private class SpinnerSelectedListener1 implements Spinner.OnItemSelectedListener
{
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
}
Spinner控件监听器1
private class SpinnerSelectedListener1 implements Spinner.OnItemSelectedListener
{
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
sharedPreferences=getSharedPreferences("date",Context.MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPreferences.edit();
editor.putString("first",OneStr[i]);//保存数据
editor.apply();
String a=sharedPreferences.getString("first","");//得到数据
Toast.makeText(MainActivity.this,a,Toast.LENGTH_SHORT).show();//显示数据
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
}
Spinner控件监听器2
private class SpinnerSelectedListener2 implements Spinner.OnItemSelectedListener
{
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
sharedPreferences=getSharedPreferences("date",Context.MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPreferences.edit();
editor.putString("second",TwoStr[i]);//保存数据
editor.apply();
String a=sharedPreferences.getString("second","");//得到数据
Toast.makeText(MainActivity.this,a,Toast.LENGTH_SHORT).show();//显示数据
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
}
Spinner控件监听器3
private class SpinnerSelectedListener3 implements Spinner.OnItemSelectedListener
{
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
sharedPreferences=getSharedPreferences("date",Context.MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPreferences.edit();
editor.putString("third",ThreeStr[i]);//保存数据
editor.apply();
String a=sharedPreferences.getString("third","");//得到数据
Toast.makeText(MainActivity.this,a,Toast.LENGTH_SHORT).show();//显示数据
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
}
绑定监听器
按钮的监听事件并把数据传至服务器
首先进行网络请求和下载okhttp3
<uses-permission android:name="android.permission.INTERNET"/>
implementation 'com.squareup.okhttp3:okhttp:3.12.1'
debugImplementation 'com.squareup.okhttp3:logging-interceptor:3.12.1'
按钮的监听事件
判断数量是否为空
success.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(e1.getText().toString().equals(""))
{
Toast.makeText(MainActivity.this,"订购失败",Toast.LENGTH_SHORT).show();
return;
}
}
});
获取数量的数据并保存
success.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(e1.getText().toString().equals(""))
{
Toast.makeText(MainActivity.this,"订购失败",Toast.LENGTH_SHORT).show();
return;
}
Toast.makeText(MainActivity.this,"订购成功",Toast.LENGTH_SHORT).show();
sharedPreferences=getSharedPreferences("date",Context.MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPreferences.edit();
editor.putString("six",e1.getText().toString());
editor.apply();
}
});
开启子线程
success.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(e1.getText().toString().equals(""))
{
Toast.makeText(MainActivity.this,"订购失败",Toast.LENGTH_SHORT).show();
return;
}
Toast.makeText(MainActivity.this,"订购成功",Toast.LENGTH_SHORT).show();
sharedPreferences=getSharedPreferences("date",Context.MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPreferences.edit();
editor.putString("six",e1.getText().toString());
editor.apply();
new Thread(new Runnable() {
@Override
public void run() {
try {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this,"网络连接成功",Toast.LENGTH_SHORT).show();
}
});
}
catch (Exception e)
{
e.printStackTrace();
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this,"网络连接失败",Toast.LENGTH_SHORT).show();
}
});
}
}
}).start();
}
});
获取全部的数据
String sr1=sharedPreferences.getString("r1","");
String sr2=sharedPreferences.getString("r2","");
String sr3=sharedPreferences.getString("r3","");
String sr4=sharedPreferences.getString("r4","");
String sr5=sharedPreferences.getString("r5","");
String sr6=sharedPreferences.getString("dd","");
String sr7=sharedPreferences.getString("rqi","");
String sr8=sharedPreferences.getString("xt","");
String sr9=sharedPreferences.getString("pl","");
String json="{\n" +
" \"car\":\""+sr1+"\",\n" +
" \"time\":\""+sr7+"\",\n" +
" \"number\":\""+sr6+"\",\n" +
" \"pail\":\""+sr9+"\",\n" +
" \"bsx\":\""+sr2+"\",\n" +
" \"lg\":\""+sr3+"\",\n" +
" \"zk\":\""+sr4+"\",\n" +
" \"sc\":\""+sr5+"\",\n" +
" \"xg\":\""+sr8+"\"\n" +
"}";
System.out.println(json);
数据上传服务器
success.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(e1.getText().toString().equals(""))
{
Toast.makeText(MainActivity.this,"订购失败",Toast.LENGTH_SHORT).show();
return;
}
Toast.makeText(MainActivity.this,"订购成功",Toast.LENGTH_SHORT).show();
sharedPreferences=getSharedPreferences("date",Context.MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPreferences.edit();
editor.putString("six",e1.getText().toString());
editor.apply();
new Thread(new Runnable() {
@Override
public void run() {
try {
//获取全部的数据
String sr1=sharedPreferences.getString("one","");
String sr2=sharedPreferences.getString("two","");
String sr3=sharedPreferences.getString("three","");
String sr4=sharedPreferences.getString("four","");
String sr5=sharedPreferences.getString("five","");
String sr6=sharedPreferences.getString("six","");
String sr7=sharedPreferences.getString("first","");
String sr8=sharedPreferences.getString("second","");
String sr9=sharedPreferences.getString("third","");
String json="{\n" +
" \"car\":\""+sr1+"\",\n" +
" \"time\":\""+sr7+"\",\n" +
" \"number\":\""+sr6+"\",\n" +
" \"pail\":\""+sr8+"\",\n" +
" \"bsx\":\""+sr2+"\",\n" +
" \"lg\":\""+sr3+"\",\n" +
" \"zk\":\""+sr4+"\",\n" +
" \"sc\":\""+sr5+"\",\n" +
" \"xg\":\""+sr9+"\"\n" +
"}";
System.out.println(json);
String url="http://192.168.1.101:5555/Interface/index/createStudentLine";
OkHttpClient okHttpClient=new OkHttpClient();
Request request=new Request.Builder()
.url(url)
.post(RequestBody.create(MediaType.parse("application/json"),json))
.build();
Response response=okHttpClient.newCall(request).execute();//执行发送的指令
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this,"网络连接成功",Toast.LENGTH_SHORT).show();
}
});
}
catch (Exception e)
{
e.printStackTrace();
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this,"网络连接失败",Toast.LENGTH_SHORT).show();
}
});
}
}
}).start();
}
});
以下是本项目的源代码:
https://download.csdn.net/download/Scxioi0/12912602