在Android中验证后,在文本视图中显示学生详细信息,可以通过以下步骤实现:
以下是一个示例代码:
// Student.java
public class Student {
private String name;
private int age;
private String gender;
public Student(String name, int age, String gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
// getter and setter methods
// ...
}
// activity_main.xml
<TextView
android:id="@+id/studentDetailsTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
// MainActivity.java
public class MainActivity extends AppCompatActivity {
private EditText nameEditText;
private EditText ageEditText;
private EditText genderEditText;
private TextView studentDetailsTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nameEditText = findViewById(R.id.nameEditText);
ageEditText = findViewById(R.id.ageEditText);
genderEditText = findViewById(R.id.genderEditText);
studentDetailsTextView = findViewById(R.id.studentDetailsTextView);
Button displayButton = findViewById(R.id.displayButton);
displayButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = nameEditText.getText().toString();
int age = Integer.parseInt(ageEditText.getText().toString());
String gender = genderEditText.getText().toString();
// Validate the input
if (!name.isEmpty() && age > 0 && !gender.isEmpty()) {
Student student = new Student(name, age, gender);
String studentDetails = "Name: " + student.getName() + "\n"
+ "Age: " + student.getAge() + "\n"
+ "Gender: " + student.getGender();
studentDetailsTextView.setText(studentDetails);
} else {
Toast.makeText(MainActivity.this, "Invalid input", Toast.LENGTH_SHORT).show();
}
}
});
}
}
这个示例中,我们创建了一个包含学生详细信息的Student类,并在MainActivity中获取用户输入的学生信息。在点击显示按钮后,我们对输入进行验证,如果验证通过,则将学生详细信息设置到TextView中进行显示。
腾讯云相关产品和产品介绍链接地址:
领取专属 10元无门槛券
手把手带您无忧上云