我正在开展 2 项 Activity 。一个 Activity 有一个 textView 和一个按钮。 textView 显示爱好,如果用户单击按钮,将设置该爱好。当用户单击该按钮时,他/她将进入第二个 Activity ,其中有 4 个与爱好相对应的复选框。当用户从菜单中单击“完成”时,他/她将返回到第一个 Activity ,之后,选中的爱好将显示在 TextView 中,以逗号(“,”)分隔。我的问题是,当用户再次使用按钮设置爱好,但已经在 textView 中显示爱好时,第二个 Activity 中的复选框应根据 textView 中显示的内容进行初始化。我尝试过数组,甚至硬编码,但我陷入了逻辑困境。这是我的代码。假设所有 View 都已初始化:
第一个屏幕(重要的一点):
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
switch (requestCode) {
case SET_BIRTHDAY:
TextView textBirthday = (TextView) findViewById(R.id.tvBirthdayStat);
birthdayString = data.getStringExtra(Lab2_082588birthday.MONTH)
+ "/" + data.getStringExtra(Lab2_082588birthday.DAY)
+ "/" + data.getStringExtra(Lab2_082588birthday.YEAR);
textBirthday.setText(birthdayString);
break;
case SET_HOBBIES:
TextView textHobbies = (TextView) findViewById(R.id.tvHobbiesStat);
anime = data.getStringExtra(Lab2_082588hobbies.ANIME);
games = data.getStringExtra(Lab2_082588hobbies.GAMES);
movies = data.getStringExtra(Lab2_082588hobbies.MOVIES);
books = data.getStringExtra(Lab2_082588hobbies.BOOKS);
checkNull();
textHobbies.setText(hobbiesString);
break;
}
}
}
private void checkNull() {
// TODO Auto-generated method stub
if (games == null) {
hobbiesString = books + " , " + movies + " , " + anime;
}
if (anime == null) {
hobbiesString = games + " , " + books + " , " + movies;
}
if (movies == null) {
hobbiesString = games + " , " + books + " , " + anime;
}
if (books == null) {
hobbiesString = games + " , " + movies + " , " + anime;
}
if ((games == null) && (anime == null)) {
hobbiesString = books + " , " + movies;
}
if ((games == null) && (movies == null)) {
hobbiesString = books + " , " + anime;
}
if ((games == null) && (books == null)) {
hobbiesString = movies + " , " + anime;
}
if ((anime == null) && (movies == null)) {
hobbiesString = games + " , " + books;
}
if ((anime == null) && (books == null)) {
hobbiesString = games + " , " + movies;
}
if ((movies == null) && (books == null)) {
hobbiesString = games + " , " + anime;
}
if ((movies == null) && (books == null) && (anime == null)) {
hobbiesString = games;
}
if ((movies == null) && (games == null) && (anime == null)) {
hobbiesString = books;
}
if ((games == null) && (books == null) && (anime == null)) {
hobbiesString = movies;
}
if ((movies == null) && (books == null) && (games == null)) {
hobbiesString = anime;
}
}
第二个屏幕:
private void startUp() {
// TODO Auto-generated method stub
Intent startUp = getIntent();
String receivedString = startUp.getStringExtra(HOBBIES_STRING);
if (receivedString != null) {
String[] separated = receivedString.split(",");
if (separated.length > 0) {
if (separated[0].equals("Anime")) {
anime.setChecked(true);
}
/*----------------*/
if (separated[0].equals("Computer Games")) {
games.setChecked(true);
}
/*----------------*/
if (separated[0].equals("Books")) {
books.setChecked(true);
}
/*----------------*/
if (separated[0].equals("Movies")) {
movies.setChecked(true);
}
/*----------------*/
if (separated[0].equals("Anime")&& separated[0].equals("Computer Games")) {
anime.setChecked(true);
games.setChecked(true);
}
if (separated[0].equals("Anime")&&separated[0].equals("Books")) {
anime.setChecked(true);
books.setChecked(true);
}
if (separated[0].equals("Anime")&&separated[0].equals("Movies")) {
anime.setChecked(true);
movies.setChecked(true);
}
}
}
代码仍然没有按预期运行。正如您所看到的,我使用了字符串拆分和字符串数组,但是由于极端的硬编码和数组索引边界问题,使用数组使其难以处理。
最佳答案
对于 CheckNull 方法,我将使用以下内容
private void checkNull() {
hobbiesstring = "";
if (games != null)
hobbiesstring += games;
if (anime != null)
hobbiesstring += anime;
if (books != null)
hobbiesstring += books;
if (movies != null)
hobbiesstring += movies;
}
对于第二部分,我将使用以下代码
private void startUp() {
Intent startUp = getIntent();
String receivedString = startUp.getStringExtra(HOBBIES_STRING);
if (receivedString != null) {
String[] separated = receivedString.split(",");
foreach(string sep in seperated){
switch(sep){
case "Anime":
anime.setChecked(true);
break;
case "Movies":
movies.setChecked(true);
break;
case "Books":
books.setChecked(true);
break;
case "Games":
games.setChecked(true);
break;
}
}
}
}
关于java - 如何使这段代码更简洁?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11361854/