我有一個玩家可以選擇玩一個記憶匹配遊戲的不同網格大小的主要活動。這個想法是讓玩家點擊他想要的網格大小,然後點擊「開始」按鈕,該按鈕將確定玩家選擇的網格大小,然後開始與網格大小相關的特定活動。Android:確定要啓動哪個活動的按鈕?
這裏是XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="edu.uc.sumanth.preferences.MainActivity">
<Button
android:text="2x2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button2x2"
android:layout_weight="1" />
<Button
android:text="4x4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button4x4"
android:layout_weight="1" />
<Button
android:text="6x6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button6x6"
android:layout_weight="1" />
<Button
android:text="Start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnStart"
android:layout_weight="1" />
我試圖利用共享偏好的概念,我是新來的Android。 這裏是在MainActivity代碼:
public class MainActivity extends AppCompatActivity
implements View.OnClickListener {
public Button btn2x2;
public Button btn4x4;
public Button btn6x6;
public Button btnStart;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn2x2 = (Button)findViewById(R.id.button2x2);
btn2x2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String gridA = btn2x2.getText().toString();
SharedPreferences sharedPreferences = getSharedPreferences("gride-size", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("Size",gridA);
editor.commit();
}
});
btn4x4 = (Button)findViewById(R.id.button4x4);
btn4x4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String gridB = btn4x4.getText().toString();
SharedPreferences sharedPreferences = getSharedPreferences("gride-size", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("Size",gridB);
editor.commit();
}
});
btn6x6 = (Button)findViewById(R.id.button6x6);
btn6x6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String gridC = btn6x6.getText().toString();
SharedPreferences sharedPreferences = getSharedPreferences("gride-size", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("Size",gridC);
editor.commit();
}
});
btnStart = (Button)findViewById(R.id.btnStart);
btnStart.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(v.getId()==R.id.button2x2){
Intent twoByTwo = new Intent(MainActivity.this, Activity_2x2.class);
startActivity(twoByTwo);
}
else if(v.getId()==R.id.button4x4){
Intent fourByFour = new Intent(MainActivity.this, Activity_2x2.class);
startActivity(fourByFour);
}
else if(v.getId() == R.id.button6x6){
Intent sixBySix = new Intent(MainActivity.this, Activity_2x2.class);
startActivity(sixBySix);
}
else{
Toast.makeText(this,"Make Choice",Toast.LENGTH_LONG).show();
}
}
}
我的想法是真正決定用戶選擇哪個網格大小,「當用戶點擊開始按鈕」,然後啓動該活動。請幫忙。
這個工作。謝謝瑞安 –
請記住標記爲接受 –