易忘点
1、背景色加圆角
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/purple_700"></solid>
// 也可选哪个角
<corners android:radius="10dp"/>
</shape>
按钮隐藏
android:visibility="gone"
2、划分空间
price占自己的宽度,剩下给unit
<TextView
android:id="@+id/price"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="¥1010/"
android:textColor="#E53935"
android:textSize="30dp"
android:gravity="center|left"
/>
<TextView
android:id="@+id/unit"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="克"
android:textSize="30dp"
android:gravity="center|left"
/>
居中靠左
android:gravity="center|left"
actvity跳转页面
Intent intent = new Intent();
intent.setClass(login.this, enroll.class);//this前面为当前activty名称,class前面为要跳转到得activity名称
startActivity(intent);
adapter跳转页面
Intent intent = new Intent(SeeDishActivity.context, SelectDishActivity.class);
SeeDishActivity.context.startActivity(intent);
通过intent传递数据
// 传递
intent.putExtra("dishInfo",mList.getJSONObject(position).toString());
// 接收
intent = getIntent();
String dishmsg = intent.getStringExtra("dishInfo");
点击事件
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new Thread(runnable).start();
}
});
多个点击事件
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvSelect = findViewById(R.id.tvSelect);
etName = findViewById(R.id.etName);
etPhone = findViewById(R.id.etPhone);
etEmail = findViewById(R.id.etEmail);
btAdd = findViewById(R.id.btAdd);
btDel = findViewById(R.id.btDel);
// 设置监听
setListeners();
}
private void setListeners() {
OnClick onClick = new OnClick();
tvSelect.setOnClickListener(onClick);
btAdd.setOnClickListener(onClick);
btDel.setOnClickListener(onClick);
}
private class OnClick implements View.OnClickListener{
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.tvSelect:
userName = etName.getText().toString();
phone = etPhone.getText().toString();
email = etEmail.getText().toString();
adapter();
break;
java文件设置颜色
holder.left_text.setBackgroundResource(R.drawable.bg_whilt);
holder.left_text.setTextColor(Color.parseColor("#E53935"));
发送post请求
public static String post(String path, JSONObject params) throws IOException, JSONException {
String data = null;
String PATH = "http://192.168.0.37:8080";
long date = new Date().getTime();
params.put("time",date);
params.put("qm",qm(params));
System.out.println(params.toString());
try {
URL url = new URL(PATH + path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true); // 需要输出
conn.setDoInput(true); // 需要输入
conn.setUseCaches(false); // 不允许缓存
conn.setConnectTimeout(50000); // 超时时间
conn.setRequestMethod("POST");
//设置请求属性
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Charset", "UTF-8");
// conn.connect();
DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
dos.write(String.valueOf(params).getBytes());
dos.flush();
dos.close();
int resultCode = conn.getResponseCode();
if(HttpURLConnection.HTTP_OK == resultCode){
StringBuffer sb = new StringBuffer();
String readLine = new String();
BufferedReader responseReader = new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8"));
while ((readLine = responseReader.readLine())!=null){
sb.append(readLine).append(("\n"));
}
responseReader.close();
data = sb.toString();
}
}catch (Exception e){
e.printStackTrace();
}
System.out.println(data);
return data;
}
发送请求和获取返回的数据 1
Runnable runnable = new Runnable() {
@Override
public void run() {
String userName = edt1.getText().toString();
String password = Resquest.MD5(edt2.getText().toString());
JSONObject js = new JSONObject();
js.put("userName",userName);
js.put("password",password);
try {
respContent = Resquest.post("/SS_CRUD/Login",js);
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
Message msg = new Message();
Bundle data = new Bundle();
data.putString("respContent",respContent);
msg.setData(data);
handler.sendMessage(msg);
}
};
Handler handler = new Handler(){
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
Bundle data = msg.getData();
String val = data.getString("respContent");
Log.i("返回的信息",val);
if (val != null && val.contains("code")){
com.alibaba.fastjson.JSONObject backJson = JSON.parseObject(val);
String code = backJson.getString("code");
if (code.equals("0")){
Toast.makeText(login.this,"登录成功",Toast.LENGTH_SHORT).show();
String data1 = backJson.getString("data");
Log.i("信息",data1);
userPre = getSharedPreferences(SharedPreferencesName.userSharedPreferences,login.MODE_PRIVATE);
SharedPreferences.Editor editor = userPre.edit();
editor.putString("userInfo",data1);
editor.commit();
Intent intent = new Intent();
intent.setClass(login.this, MainPage.class);//this前面为当前activty名称,class前面为要跳转到得activity名称
startActivity(intent);
finish();
}else {
String msg1 = backJson.getString("msg");
Toast.makeText(login.this,msg1,Toast.LENGTH_SHORT).show();
}
}
}
};
发送请求和返回数据2
public JSONArray seeDish(String dishTypeId) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
// 发送请求
try {
respDish = Resquest.post("/SS_CRUD/SelectDishes", dishJson);
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
System.out.println("查询成功");
System.out.println("菜品的信息:" + respDish);
}
});
thread.start();
try {
thread.join();
JSONObject dishBack = JSONObject.parseObject(respDish);
JSONObject dd = dishBack.getJSONObject("data");
JSONArray dishArr = dd.getJSONArray("list");
dishArray = new JSONArray();
for (int i = 0; i < dishArr.size(); i++) {
dishArray.add(dishArr.getJSONObject(i));
}
System.out.println("菜品的长度:" + dishArray.size());
} catch (InterruptedException e) {
e.printStackTrace();
}
return dishArray;
}
使用存储数据
userPre = getSharedPreferences(SharedPreferencesName.userSharedPreferences,login.MODE_PRIVATE);
SharedPreferences.Editor editor = userPre.edit();
editor.putString("userInfo",data1);
editor.commit();
获取储存的数据
SharedPreferences userInfo = getSharedPreferences(SharedPreferencesName.userSharedPreferences, MODE_PRIVATE);
user = userInfo.getString("userInfo", "");
RecyclerView 的使用
build.gradle文件:
implementation ‘com.android.support:design:30.0.0’
1、一个有四个文件 依次去写
1、activity文件
2、activity对应的xml布局文件(整体布局)
3、adapter文件(重要!!!)
4、item.xml文件(每一块的布局)
下面是例子
1、activity对应的xml布局文件(整体布局)
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_weight="1"
>
// 两个RecyclerView样式
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/relfList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFC9CBCF"
android:layout_weight="4"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rightList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"/>
</LinearLayout>
左边的relfList每一个item的布局
<TextView
android:id="@+id/left_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="2dp"
android:layout_weight="1"
android:gravity="center"
android:background="#FFC9CBCF"
android:text="123"
android:textColor="#000"
android:textSize="20dp" />
右边的t每一个item的布局
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="140dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/guigui"
android:layout_weight="1"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="0.5">
<TextView
android:id="@+id/dishName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="菜品"
android:textSize="30dp"
android:gravity="center|left"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal"
>
<TextView
android:id="@+id/price"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="¥1010/"
android:textColor="#E53935"
android:textSize="30dp"
android:gravity="center|left"
/>
<TextView
android:id="@+id/unit"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="克"
android:textSize="30dp"
android:gravity="center|left"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
adpter文件
// 点击按钮,显示对应的数据 dishAdapter 指向SeeDishActivity.dishAdapter; 很重要
public class SeeLeftDIshAdapter extends RecyclerView.Adapter<SeeLeftDIshAdapter.MyHolder>{
private JSONArray mList; // 数据源
int index = 0;
SeeRightDishAdapter dishAdapter;
SeeLeftDIshAdapter(JSONArray list){
mList = list;
}
// 创建ViewHolder并返回,后续item布局里控件都是从ViewHolder中取出
@NonNull
@Override
public SeeLeftDIshAdapter.MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
// 将我们自定义的item 每一块的布局R.layout 转为View
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.left_item,parent,false);
// 将view传递给我们自定义的ViewHolder
MyHolder holder = new MyHolder(view);
return holder;
}
// 通过提供的ViewHolder,将数据绑定到ViewHolder
@Override
public void onBindViewHolder(@NonNull SeeLeftDIshAdapter.MyHolder holder, int position) {
holder.left_text.setText(mList.getJSONObject(position).get("dishesTypeName").toString());
holder.left_text.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String dishId = mList.getJSONObject(position).get("dishesType").toString();
index = position;
JSONArray jsonArray = new SeeDishActivity().seeDish(dishId);
Log.i("单点菜品类型:",jsonArray.toString());
// 点击按钮,显示对应的数据 dishAdapter 指向SeeDishActivity.dishAdapter;
dishAdapter= SeeDishActivity.dishAdapter;
dishAdapter.getList(jsonArray);
dishAdapter.notifyDataSetChanged();
// 刷新页面
notifyDataSetChanged();
}
});
// 如果点击 的变化
if (index == position){
// 改变颜色
holder.left_text.setBackgroundResource(R.drawable.bg_whilt);
holder.left_text.setTextColor(Color.parseColor("#E53935"));
holder.left_right.setBackgroundResource(R.color.orange);
}else {
holder.left_text.setBackgroundResource(R.color.w);
holder.left_text.setTextColor(Color.parseColor("#000000"));
holder.left_right.setBackgroundResource(R.color.w);
}
}
// item的个数
@Override
public int getItemCount() {
return mList.size();
}
class MyHolder extends RecyclerView.ViewHolder{
TextView left_text;
TextView left_right;
public MyHolder(@NonNull View itemView) {
super(itemView);
left_text = itemView.findViewById(R.id.left_text);
left_right = itemView.findViewById(R.id.left_right);
}
}
}
activity文件
public class SeeDishActivity extends AppCompatActivity {
public static String shopId;
//菜品类型的返回信息
String respDishType = "";
// 菜品的返回信息
String respDish = "";
JSONArray dishArray;
JSONArray arrayDishType;
JSONArray dishesTypeList;
JSONArray dishListArr;
String user;
LinearLayoutManager dishesTypeLM;
LinearLayoutManager dishesLM;
SeeLeftDIshAdapter dishType;
static SeeRightDishAdapter dishAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_see_dish);
// 获取登录用户信息
SharedPreferences userInfo = getSharedPreferences(SharedPreferencesName.userSharedPreferences, MODE_PRIVATE);
user = userInfo.getString("userInfo", "");
System.out.println("用户信息:" + user);
JSONObject userJson = JSONObject.parseObject(user);
shopId = userJson.getString("shopId");
System.out.println("店铺ID:" + shopId);
}
@Override
protected void onResume() {
super.onResume();
RecyclerView dishTypeRV = findViewById(R.id.relfList);
// 初始化数据
dishesTypeList = seeDishType();
System.out.println("111313:" + dishesTypeList.toString());
// 菜品类型的布局
// 创建布局管理器,垂直设置LinearLayoutManager.Vertical,水平设置LinearLayoutManager.Hoeizontal
dishesTypeLM = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
// 创建适配器,将数据传递给适配器
dishType = new SeeLeftDIshAdapter(dishesTypeList);
// 设置布局管理器
dishTypeRV.setLayoutManager(dishesTypeLM);
// 设置适配器
dishTypeRV.setAdapter(dishType);
// // 菜品的布局
RecyclerView dishRV = findViewById(R.id.rightList);
dishListArr = seeDish("");
Log.i("菜品查询的结果:",dishListArr.toString());
dishesLM = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
dishAdapter = new SeeRightDishAdapter(dishListArr);
dishRV.setLayoutManager(dishesLM);
dishRV.setAdapter(dishAdapter);
}
public JSONArray seeDish(String dishTypeId) {
System.out.println("菜品类型的Id"+ dishTypeId);
System.out.println("店铺Id:"+SeeDishActivity.shopId);
// 放入菜品的参数
JSONObject dishJson = new JSONObject();
dishJson.put("shopId", SeeDishActivity.shopId);
dishJson.put("dishTypeId", dishTypeId);
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
// 发送请求
try {
respDish = Resquest.post("/SS_CRUD/SelectDishes", dishJson);
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
System.out.println("查询成功");
System.out.println("菜品的信息:" + respDish);
}
});
thread.start();
try {
thread.join();
JSONObject dishBack = JSONObject.parseObject(respDish);
JSONObject dd = dishBack.getJSONObject("data");
JSONArray dishArr = dd.getJSONArray("list");
dishArray = new JSONArray();
for (int i = 0; i < dishArr.size(); i++) {
dishArray.add(dishArr.getJSONObject(i));
}
System.out.println("菜品的长度:" + dishArray.size());
} catch (InterruptedException e) {
e.printStackTrace();
}
return dishArray;
}
private JSONArray seeDishType() {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
// 查询菜品类型的参数
JSONObject js = new JSONObject();
js.put("shopId", shopId);
// 发送请求
try {
respDishType = Resquest.post("/SS_CRUD/SeeDishesType", js);
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
System.out.println("菜品类型信息:" + respDishType);
}
});
thread.start();
try {
thread.join();
JSONObject backDish = JSONObject.parseObject(respDishType);
JSONObject da = backDish.getJSONObject("data");
JSONArray list = da.getJSONArray("list");
JSONObject quanbu = new JSONObject();
quanbu.put("dishesType", "");
quanbu.put("dishesTypeName", "全部菜品");
quanbu.put("shopId", "");
System.out.println("quanbu:" + quanbu.toString());
arrayDishType = new JSONArray();
arrayDishType.add(quanbu);
for (int i = 0; i < list.size(); i++) {
System.out.println("list的内容" + (list.getJSONObject(i).toString()));
arrayDishType.add(list.getJSONObject(i));
}
} catch (InterruptedException e) {
e.printStackTrace();
}
return arrayDishType;
}
}
往item放图片
前提:加载glide框架
glide框架介绍
SeeDishActivity.context何意?
1、在SeeDishActivity中定义:static Context context;
2、在SeeDishActivity中赋值:context = SeeDishActivity.this;
3、在adapter中获取:SeeDishActivity.contex
// 显示图片,没有图片的显示默认图片
if (!mList.getJSONObject(position).get("dishesImg").toString().equals("")){
String url = "http://192.168.0.37:8080/SS_CRUD/SeeImg?"+ mList.getJSONObject(position).get("dishesImg").toString();
Glide.with(SeeDishActivity.context).load(url).into(holder.image);
}else {
String url = "http://192.168.0.37:8080/SS_CRUD/SeeImg?1.png";
Glide.with(SeeDishActivity.context).load(url).into(holder.image);
}
adapter文件中点击按钮跳转页面
SeeDishActivity.context何意?
1、在SeeDishActivity中定义:static Context context;
2、在SeeDishActivity中赋值:context = SeeDishActivity.this;
3、在adapter中获取:SeeDishActivity.contex
Intent intent;
intent = new Intent(SeeDishActivity.context, enroll.class);
SeeDishActivity.context.startActivity(intent);
**
自定义弹出框
**
lay_adduser 弹出框的样式
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.lay_adduser,null);
EditText etName = view.findViewById(R.id.etName);
EditText etPhone = view.findViewById(R.id.etPhone);
EditText etAddress = view.findViewById(R.id.etAddress);
EditText etEmile = view.findViewById(R.id.etEmile);
builder.setPositiveButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).setNegativeButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.setView(view).show();
alterDialog点击确定按钮不消失,满足条件才消失
View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.lay_adduser,null);
EditText etName = view.findViewById(R.id.etName);
EditText etPhone = view.findViewById(R.id.etPhone);
EditText etAddress = view.findViewById(R.id.etAddress);
EditText etEmile = view.findViewById(R.id.etEmile);
AlertDialog dialog = new AlertDialog.Builder(MainActivity.this).setView(view).setPositiveButton("确定", null)
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).create();
//这里必须要先调show()方法,后面的getButton才有效
dialog.show();
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (TextUtils.isEmpty(etName.getText())) {
Toast.makeText(MainActivity.this,"姓名不能为空不对!",Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(etAddress.getText())) {
Toast.makeText(MainActivity.this,"地址不能为空不对!",Toast.LENGTH_LONG).show();
return;
}
if (!isMobile(etPhone.getText().toString())) {
Toast.makeText(MainActivity.this,"手机格式不对!",Toast.LENGTH_LONG).show();
return;
}
if (!isEmail(etEmile.getText().toString())) {
Toast.makeText(MainActivity.this,"邮箱格式不对!",Toast.LENGTH_LONG).show();
return;
}
String code = addUser(etName.getText().toString(),etPhone.getText().toString(),etAddress.getText().toString(),etEmile.getText().toString());
if (!code.equals("0000")){
Toast.makeText(MainActivity.this,"用户名重复不对!",Toast.LENGTH_LONG).show();
}
adapter();
dialog.dismiss();
}
});
alertDialog 将数组中的数据弹出框的形式罗列出来,进行选择
// 点击单位出现弹窗 String[] unitStrArr = new String[]{1,2,4,3};
private void alertDialog() {
builder = new AlertDialog.Builder(UpdateDishActivity.this);
// 设置参数
builder.setTitle("选择单位");
builder.setSingleChoiceItems(unitStrArr, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
numUnit = which;
}
})
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
tvDishUnit.setText(unitStrArr[numUnit]);
unitid = unitArr.getJSONObject(numUnit).getInteger("unitId");
}
})
.setNegativeButton("取消",null).show();
}
acticity点击按钮操作adapter中的所有item
不能在adapter中引用activity的按钮变量,在adapter中执行点击事件!
avtivity文件
public static TextView uptDishType;
dishType: // 创建适配器,将数据传递给适配器
SeedishTypeAdapter dishType = new SeedishTypeAdapter(dishTypeArr);
-----------------------------------------------------
uptDishType = findViewById(R.id.uptDishType);
uptDishType.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (uptDishType.getText().equals("编辑")){
uptDishType.setText("完成编辑");
}else {
uptDishType.setText("编辑");
}
dishType.notifyDataSetChanged();
}
});
adapter
// 点击编辑
if(uptDishType.getText().equals("完成编辑")){
holder.biImg.setVisibility(View.VISIBLE);
holder.delImg.setVisibility(View.VISIBLE);
holder.img.setVisibility(View.GONE);
}else {
holder.biImg.setVisibility(View.GONE);
holder.delImg.setVisibility(View.GONE);
}
2378

被折叠的 条评论
为什么被折叠?



