帮我写一个Android studio开发的访客登录界面。要求包含一个textview"访客登录界面",输入姓名和学号,选择性别男女,下拉框选择学院和专业,登录按钮跳转至另一个界面
时间: 2024-02-01 16:12:09 浏览: 115
利用Android Studio实现登录界面
5星 · 资源好评率100%
好的,以下是一个简单的访客登录界面的布局代码:
```xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/title_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="访客登录界面"
android:textSize="24sp"
android:layout_centerHorizontal="true"
android:layout_marginTop="24dp"/>
<EditText
android:id="@+id/name_edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入姓名"
android:layout_below="@id/title_text"
android:layout_marginTop="48dp"/>
<EditText
android:id="@+id/id_edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入学号"
android:layout_below="@id/name_edit"/>
<RadioGroup
android:id="@+id/gender_radio"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/id_edit"
android:orientation="horizontal">
<RadioButton
android:id="@+id/male_radio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男"/>
<RadioButton
android:id="@+id/female_radio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女"/>
</RadioGroup>
<Spinner
android:id="@+id/college_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/gender_radio"
android:entries="@array/college_array"
android:prompt="@string/college_prompt"/>
<Spinner
android:id="@+id/major_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/college_spinner"
android:entries="@array/major_array"
android:prompt="@string/major_prompt"/>
<Button
android:id="@+id/login_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登录"
android:layout_below="@id/major_spinner"
android:layout_marginTop="48dp"/>
</RelativeLayout>
```
接下来,我们需要在Java代码中处理登录按钮的点击事件,并跳转至另一个界面。以下是一个简单的实现:
```java
public class LoginActivity extends AppCompatActivity {
private EditText mNameEdit;
private EditText mIdEdit;
private RadioGroup mGenderRadio;
private Spinner mCollegeSpinner;
private Spinner mMajorSpinner;
private Button mLoginButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
mNameEdit = findViewById(R.id.name_edit);
mIdEdit = findViewById(R.id.id_edit);
mGenderRadio = findViewById(R.id.gender_radio);
mCollegeSpinner = findViewById(R.id.college_spinner);
mMajorSpinner = findViewById(R.id.major_spinner);
mLoginButton = findViewById(R.id.login_button);
mLoginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = mNameEdit.getText().toString();
String id = mIdEdit.getText().toString();
int genderId = mGenderRadio.getCheckedRadioButtonId();
String gender = "";
if (genderId == R.id.male_radio) {
gender = "男";
} else if (genderId == R.id.female_radio) {
gender = "女";
}
String college = mCollegeSpinner.getSelectedItem().toString();
String major = mMajorSpinner.getSelectedItem().toString();
Intent intent = new Intent(LoginActivity.this, ProfileActivity.class);
intent.putExtra("name", name);
intent.putExtra("id", id);
intent.putExtra("gender", gender);
intent.putExtra("college", college);
intent.putExtra("major", major);
startActivity(intent);
}
});
}
}
```
在上面的代码中,我们首先获取了用户输入的姓名、学号、性别、学院和专业信息,然后创建了一个跳转至ProfileActivity的Intent,并将这些信息作为Extra传递给了ProfileActivity。最后通过startActivity方法实现了跳转。需要在Manifest文件中声明ProfileActivity的界面。
```java
public class ProfileActivity extends AppCompatActivity {
private TextView mNameText;
private TextView mIdText;
private TextView mGenderText;
private TextView mCollegeText;
private TextView mMajorText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
mNameText = findViewById(R.id.name_text);
mIdText = findViewById(R.id.id_text);
mGenderText = findViewById(R.id.gender_text);
mCollegeText = findViewById(R.id.college_text);
mMajorText = findViewById(R.id.major_text);
Intent intent = getIntent();
String name = intent.getStringExtra("name");
String id = intent.getStringExtra("id");
String gender = intent.getStringExtra("gender");
String college = intent.getStringExtra("college");
String major = intent.getStringExtra("major");
mNameText.setText(name);
mIdText.setText(id);
mGenderText.setText(gender);
mCollegeText.setText(college);
mMajorText.setText(major);
}
}
```
在上面的代码中,我们首先获取了从LoginActivity传递过来的Extra信息,并将它们显示在TextView中。这样,当用户登录成功后,就能看到他们的个人信息了。
希望这个简单的示例能够帮助你完成访客登录界面的开发。
阅读全文