gpt4 book ai didi

java - 下面一行 - 在不使用相同名称的情况下制作三个微调器和玩家添加或选择

转载 作者:行者123 更新时间:2023-11-30 04:05:37 24 4
gpt4 key购买 nike

大家早上好。又是我,大卫·迪马兰塔。我是来提问的我需要创建一个程序,如果所有三个玩家的名字都不同,则确认将在 toast 上说“过程完成”。但相反,即使所有人都没有相同的名字。

步骤:

  1. 每个玩家从旋转器中选择一个名字。
  2. 将值写入字符串。
  3. 单击按钮时,该过程会评估相似性别名(名称)。
  4. 如果相似,至少两个,toast 消息会说“请指定不同的用户名。”
  5. 如果没有,那么它就完成了。

而且,我的 Activity 名称是“Player_3_at_Spinner_Menu.java”。这是我在该类(class)下第一部分的代码:

//Spinners for Players
private Spinner spinner_1;
private Spinner spinner_2;
private Spinner spinner_3;

//Button to Start
private Button play_it;

//For Displaying Text
private String SUMMON_PICK_UP_1, SUMMON_PICK_UP_2, SUMMON_PICK_UP_3;
private String cplayer_1, cplayer_2, cplayer_3;

//Text Response from a Spinner
public final static String EXTRA_MESSAGE_1 = "com.example.databasetestvertwo.MESSAGE1";
public final static String EXTRA_MESSAGE_2 = "com.example.databasetestvertwo.MESSAGE2";
public final static String EXTRA_MESSAGE_3 = "com.example.databasetestvertwo.MESSAGE3";

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.players_3);

//Searching for ID... (Button)
play_it = (Button) findViewById(R.id.START_GAME);

//Adding listener to the button(s).
play_it.setOnClickListener(new trigger_happy_start());

//Call from the Database_Handler.class to call the database.
Database_Handler db = new Database_Handler(getApplicationContext());

//Then, load the content and...
loadSpinnerData();
}




//Insert the value from the database into each of the spinners.
private void loadSpinnerData()
{

//Initialize the spinners.
spinner_1 = (Spinner) findViewById(R.id.player_1_spinner);
spinner_2 = (Spinner) findViewById(R.id.player_2_spinner);
spinner_3 = (Spinner) findViewById(R.id.player_3_spinner);

Database_Handler db = new Database_Handler(getApplicationContext());

List<String> lables = db.getAllLabels();

//Creating an adapter for the spinner...
ArrayAdapter<String> data_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, lables);

data_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

//Inserts the spinners by a database.
spinner_1.setAdapter(data_adapter);
spinner_2.setAdapter(data_adapter);
spinner_3.setAdapter(data_adapter);

}




//Action applied if a user chose this item. (Player 1)
public class response_1 implements OnItemSelectedListener
{

public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{

SUMMON_PICK_UP_1 = parent.getItemAtPosition(position).toString();

Toast.makeText(parent.getContext(), "You selected: " + SUMMON_PICK_UP_1, Toast.LENGTH_SHORT).show();

}

public void onNothingSelected(AdapterView<?> arg0)
{

//Do nothing. I guess...

}

}




//Action applied if a user chose this item. (Player 2)
public class response_2 implements OnItemSelectedListener
{

public void onItemSelected(AdapterView<?> parent_2, View view, int position, long id)
{

SUMMON_PICK_UP_2 = parent_2.getItemAtPosition(position).toString();

Toast.makeText(parent_2.getContext(), "You selected: " + SUMMON_PICK_UP_2, Toast.LENGTH_SHORT).show();


}

public void onNothingSelected(AdapterView<?> arg0)
{

// TODO Auto-generated method stub

}

}




//Action applied if a user chose this item. (Player 3)
public class response_3 implements OnItemSelectedListener
{

public void onItemSelected(AdapterView<?> parent_3, View view, int position, long id)
{

SUMMON_PICK_UP_3 = parent_3.getItemAtPosition(position).toString();

Toast.makeText(parent_3.getContext(), "You selected: " + SUMMON_PICK_UP_2, Toast.LENGTH_SHORT).show();


}

public void onNothingSelected(AdapterView<?> arg0)
{

// TODO Auto-generated method stub

}

}

并且,这里是检查所有或至少两个玩家是否有相同名字的代码,该过程警告他们不要在此 Activity 下也使用相同的名字。这是我的代码:

private class trigger_happy_start implements OnClickListener
{

public void onClick(View v)
{

//Checks if the names assigned on each spinner have a the same name.
if
(
SUMMON_PICK_UP_1 == SUMMON_PICK_UP_2 ||
SUMMON_PICK_UP_1 == SUMMON_PICK_UP_3 ||
SUMMON_PICK_UP_2 == SUMMON_PICK_UP_3
)
{
Toast.makeText(getApplicationContext(), "Please specify a different username.", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getApplicationContext(), "Process complete, idiot.", Toast.LENGTH_SHORT).show();
}

}

}

最佳答案

我没有发现您的应用程序的逻辑有任何问题。

但是,您忘记设置 Spinner 的 onItemSelected:

spinner1.setOnItemSelectedListener( new response_1());
spinner2.setOnItemSelectedListener( new response_2());
spinner3.setOnItemSelectedListener( new response_3());

关于java - 下面一行 - 在不使用相同名称的情况下制作三个微调器和玩家添加或选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11732668/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com