【发布时间】:2016-11-24 01:42:31
【问题描述】:
我有一个不同网格大小的主要活动,玩家可以选择玩记忆匹配游戏。这个想法是让玩家单击他想要的网格大小,然后单击“开始”按钮,该按钮将确定玩家选择的网格大小,然后开始与网格大小相关的特定活动。
这是 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();
}
}
}
我的想法是真正确定用户选择的网格大小,“当用户单击开始按钮时”,然后开始该活动。请帮忙。
【问题讨论】: