Android studio的登录代码,登录后跳到一个新界面,新界面为一张课程表,课程表的布局使用gridview控件,并且控件下面有三个按钮,能对课程表做出修改,删除,查询,修改界面能修改课程时间,地点以及添加课程名称,课程时间,课程地点,删除界面能删除选定课程,并且更新修改后的课程表的代码以及界面布局代码
时间: 2023-10-03 18:03:56 浏览: 31
以下是一个简单的Android Studio登录代码,登录成功后跳转到课程表界面:
### LoginActivity.java
```
public class LoginActivity extends AppCompatActivity {
private EditText etUsername, etPassword;
private Button btnLogin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
etUsername = findViewById(R.id.et_username);
etPassword = findViewById(R.id.et_password);
btnLogin = findViewById(R.id.btn_login);
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String username = etUsername.getText().toString();
String password = etPassword.getText().toString();
// TODO: 进行登录验证
Intent intent = new Intent(LoginActivity.this, CourseActivity.class);
startActivity(intent);
finish();
}
});
}
}
```
### activity_login.xml
```
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/et_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="用户名"
android:inputType="text" />
<EditText
android:id="@+id/et_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/et_username"
android:hint="密码"
android:inputType="textPassword" />
<Button
android:id="@+id/btn_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/et_password"
android:text="登录" />
</RelativeLayout>
```
接下来是课程表界面的布局和代码:
### CourseActivity.java
```
public class CourseActivity extends AppCompatActivity {
private GridView gridView;
private Button btnAdd, btnModify, btnDelete;
private List<Course> courseList;
private CourseAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_course);
gridView = findViewById(R.id.grid_view);
btnAdd = findViewById(R.id.btn_add);
btnModify = findViewById(R.id.btn_modify);
btnDelete = findViewById(R.id.btn_delete);
courseList = new ArrayList<>();
adapter = new CourseAdapter(this, courseList);
gridView.setAdapter(adapter);
// TODO: 加载课程表数据
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// TODO: 打开添加课程界面
}
});
btnModify.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// TODO: 打开修改课程界面
}
});
btnDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// TODO: 删除选中课程
}
});
}
}
```
### activity_course.xml
```
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<GridView
android:id="@+id/grid_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="5"
android:horizontalSpacing="1dp"
android:verticalSpacing="1dp"
android:stretchMode="columnWidth" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/grid_view"
android:orientation="horizontal">
<Button
android:id="@+id/btn_add"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="添加" />
<Button
android:id="@+id/btn_modify"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="修改" />
<Button
android:id="@+id/btn_delete"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="删除" />
</LinearLayout>
</RelativeLayout>
```
最后是课程表的适配器和数据模型:
### CourseAdapter.java
```
public class CourseAdapter extends BaseAdapter {
private Context context;
private List<Course> courseList;
public CourseAdapter(Context context, List<Course> courseList) {
this.context = context;
this.courseList = courseList;
}
@Override
public int getCount() {
return courseList.size();
}
@Override
public Object getItem(int position) {
return courseList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.item_course, parent, false);
}
TextView tvName = convertView.findViewById(R.id.tv_name);
TextView tvTime = convertView.findViewById(R.id.tv_time);
TextView tvLocation = convertView.findViewById(R.id.tv_location);
Course course = courseList.get(position);
tvName.setText(course.getName());
tvTime.setText(course.getTime());
tvLocation.setText(course.getLocation());
return convertView;
}
}
```
### Course.java
```
public class Course {
private String name;
private String time;
private String location;
public Course(String name, String time, String location) {
this.name = name;
this.time = time;
this.location = location;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
}
```
希望这份代码能够帮到你。