【发布时间】:2018-07-19 14:06:40
【问题描述】:
这是安卓工作室。我不知道为什么我无法使用按钮从 MainActivity.java 类中打开 SelectPlayer.java 和 SelectPlayer2.java。它与我使用 Intent String data = i.getStringExtra("text_key"); 的方式有关。我相信从我的 AddPLayer.java 类中检索字符串。我可以使用 Edittext 填充列表视图,但是 MainActivity 中的按钮将不会打开我的 SelectPlayer 和 SelectPlayer2 活动。请帮忙。
MainActivity UI 创建用于导航到 5 个菜单的按钮
public class MainActivity extends AppCompatActivity {
//Declare variables
Button gameStart;
Button addPlayer;
Button selectPlayer1;
Button selectPlayer2;
//Create OnCLickListener for buttons
View.OnClickListener listener1 = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Use listener with switch case scenarios to navigate to different UIs
listener1 = new OnClickListener() {
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.startgame:
startActivity(new Intent(MainActivity.this,GameEmulator.class));
break;
case R.id.addplayerbutton:
startActivity(new Intent(MainActivity.this,AddPlayer.class));
break;
case R.id.selectplayer1:
startActivity(new Intent(MainActivity.this,SelectPlayer.class));
break;
case R.id.selectplayer2:
startActivity(new Intent(MainActivity.this,SelectPlayer2.class));
}
}
};
//Setting content to id buttons and set listeners
setContentView(R.layout.activity_main);
gameStart= findViewById(R.id.startgame);
gameStart.setOnClickListener(listener1);
addPlayer= findViewById(R.id.addplayerbutton);
addPlayer.setOnClickListener(listener1);
selectPlayer1= findViewById(R.id.selectplayer1);
selectPlayer1.setOnClickListener(listener1);
selectPlayer2= findViewById(R.id.selectplayer2);
selectPlayer2.setOnClickListener(listener1);
}
}
Addplayer 类使用 Intent 添加玩家来分隔活动
public class AddPlayer extends Activity{
//Declare buttons and edittext
Button listButtonplayer1;
Button listButtonplayer2;
EditText playerIn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.playerinput);
//Identify buttons and editext
playerIn = (EditText) findViewById(R.id.inputname);
listButtonplayer1 = (Button)findViewById(R.id.addbutton);
listButtonplayer1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Using intent to send input to SelectPLayer Class
Intent i = new Intent(AddPlayer.this,SelectPlayer.class);
//Using string text_key to get and send string through intent
i.putExtra("text_key", playerIn.getText().toString());
//Start intent activity
startActivity(i);
}
});
//Second button I am trying to use for SelectPLayer2 activity only.
listButtonplayer2 = (Button)findViewById(R.id.addbutton2);
listButtonplayer2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Using intent to send input to SelectPLayer2 Class
Intent in = new Intent(AddPlayer.this, SelectPlayer2.class);
in.putExtra("text_key", playerIn.getText().toString());
startActivity(in);
}
});
}
}
选择播放器 1 的类
public class SelectPlayer extends Activity {
//Public static array list with adapter to crete the array and reference for listview being sent by intent from AddPLayer class
public static ArrayList<String> list = new ArrayList<>();
public static ArrayAdapter<String> adapter;
public static ListView selectView;
TextView title;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.selectplayer);
//Identify selectview ListView
selectView = findViewById(R.id.selectview);
selectView.setClickable(true);
selectView.setVisibility(View.VISIBLE);
//Using adapter for ListView menu
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
selectView.setAdapter(adapter);
selectView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String item = (String) parent.getItemAtPosition(position); // finding the item which has been clicked by the user
Intent intent = new Intent(SelectPlayer.this, GameEmulator.class); //starting an intent to call GameEmulator Activity
intent.putExtra(GameEmulator.value, item);// Putting the value clicked by user in intent
startActivity(intent); // starting GameEmulator Activity
}
});
//Using intent to retrieve string from AddPlayer Activity
Intent i = getIntent();
String data = i.getStringExtra("text_key");
list.add(data);
changeList();
}
public void changeList()
{
adapter.notifyDataSetChanged();
}
}
选择播放器 2 的类
public class SelectPlayer2 extends Activity {
//Public static array list with adapter to crete the array and reference for listview being sent by intent from AddPLayer class
public static ArrayList<String> list1 = new ArrayList<>();
public static ArrayAdapter<String> adapter1;
Intent in;
ListView selectView2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.selectplayer2);
selectView2 = (ListView) findViewById(R.id.selectview1);
selectView2.setClickable(true);
selectView2.setVisibility(View.VISIBLE);
//Using adapter for ListView menu
adapter1 = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,list1);
selectView2.setAdapter(adapter1);
selectView2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String item =(String) parent.getItemAtPosition(position); // finding the item which has been clicked by the user
Intent in = new Intent(SelectPlayer2.this, GameEmulator.class); //starting an intent to call GameEmulator Activity
in.putExtra(GameEmulator.value, item);// Putting the value clicked by user in intent
startActivity(in); // starting GameEmulator Activity
}
});
//Using intent to retrieve string from AddPlayer Activity
in = getIntent();
String data = in.getStringExtra("text_key");
list1.add(data);
changeList();
}
public static void changeList()
{
adapter1.notifyDataSetChanged();
}
}
【问题讨论】:
-
点击
MainActivity中的selectplayer1和selectplayer2按钮究竟会发生什么? -
应用程序关闭并必须重新启动,应用程序只会让我通过我的按钮从 MainAcitivty 访问 AddPLayer Activity。当我尝试从主屏幕 MainActivity 访问 SelectPLayer 或 SelectPlayer2 时,应用程序关闭。我很困惑,因为 AddPLayer 是目前唯一可用的按钮。
-
您需要在
case语句中传递额外信息和意图。
标签: android listview android-edittext