2024/3/20

今日任务:继续完成后端代码实现发表动态功能

存在问题:无

package com.example.team;

import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.R;
import com.example.team.dao.UserDao;


public class SpaceFragment extends Fragment {
private EditText spaceEditText;
private String username;
private String name;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_space, container, false);

spaceEditText = view.findViewById(R.id.editText);
Button searchStationButton = view.findViewById(R.id.button3);
Button button2 = view.findViewById(R.id.button2);
TextView textView = view.findViewById(R.id.textView);

// 设置字体
Typeface customFont = TypefaceUtil.getCustomFont(requireContext(), "xiniu.ttf"); // 替换为您的字体文件名
spaceEditText.setTypeface(customFont);
searchStationButton.setTypeface(customFont);
button2.setTypeface(customFont);
textView.setTypeface(customFont);

// 获取传递过来的用户名
if (getArguments() != null) {
username = getArguments().getString("username");
name = getArguments().getString("name");
}

button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String cspace = spaceEditText.getText().toString();

new Thread() {
@Override
public void run() {
int msg = 0;
UserDao userDao = new UserDao(requireContext());
com.example.team.entity.SpacePlus user = userDao.findUser1(username);
if (user != null) {
com.example.team.entity.Space space = new com.example.team.entity.Space();
space.setSpace(cspace);
space.setUsername(user.getUsername());
space.setName(user.getName());

boolean flag = userDao.space(space);
if (flag) {
msg = 2;
}
} else {
msg = 1;
}
hand.sendEmptyMessage(msg);
}
}.start();
}
});

searchStationButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getContext(), SpaceView.class);
intent.putExtra("username", username);
intent.putExtra("name", name);
startActivity(intent);
}
});

return view;
}

final Handler hand = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what == 0) {
Toast.makeText(requireContext(), "发表失败", Toast.LENGTH_LONG).show();
} else if (msg.what == 1) {
Toast.makeText(requireContext(), "用户名:" + username, Toast.LENGTH_LONG).show();
}
}
};
}
posted @ 2024-03-20 22:29  芊羽鱼  阅读(9)  评论(0)    收藏  举报