Ric*_*ers 5 java android android-intent sharedpreferences
我有一个问题是通过另一个Activity设置按钮可见性
代码说明:
第一, menu.xml
<Button
android:id="@+id/f1"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginRight="10dp"
android:background="@drawable/button1"
android:visibility="visible" />
<ImageView
android:id="@+id/f2lock"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:src="@drawable/levellocked"
android:visibility="visible" />
<Button
android:id="@+id/f2"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@drawable/button2"
android:visibility="gone" />
Run Code Online (Sandbox Code Playgroud)
f2用于意图leveltwo.class但仍然设置为GONE的
按钮f2lock是用于levellocked的ImageView
第二,menu.java
public class menu extends Activity {
Button f1, f2;
ImageView f2lock;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.famouslevel);
f1 =(Button)findViewById(R.id.f1);
f1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
// TODO Auto-generated method stub
Intent level1 = new Intent ();
level1.setClassName ("com.example.game", "com.example.game.levelone");
startActivityForResult (level1, 0);
}
});
}
public void onActivityResult (int requestCode, int resultCode, Intent level1){
super.onActivityResult (requestCode, resultCode, level1);
f2=(Button)findViewById(R.id.f2);
f2lock=(ImageView)findViewById(R.id.f2lock);
switch (resultCode) {
case 2: f2.setVisibility(View.VISIBLE);
f2lock.setVisibility(View.GONE);
}
f2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
// TODO Auto-generated method stub
Intent level2 = new Intent ();
level2.setClassName ("com.example.game", "com.example.game.leveltwo");
startActivityForResult (level2, 0);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.splashscreen, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
Run Code Online (Sandbox Code Playgroud)
以下代码levelone.java用Result 调用
所以levelone.java我把这样的代码
public void onClick(View v){
setResult (2);
finish();
}
});
Run Code Online (Sandbox Code Playgroud)
代码函数是将Result(2)发送到menu.classwhen level.class();
public void onActivityResult (int requestCode, int resultCode, Intent level1){
super.onActivityResult (requestCode, resultCode, level1);
f2=(Button)findViewById(R.id.f2);
f2lock=(ImageView)findViewById(R.id.f2lock);
switch (resultCode) {
case 2: f2.setVisibility(View.VISIBLE);
f2lock.setVisibility(View.GONE);
}
Run Code Online (Sandbox Code Playgroud)
上面的代码是从函数接收结果(2)levelone.class和执行case 2函数
问题是如何在案例2中使用和设置SharedPreferences?所以f2和f2lock的可见性将会保存
因为我已经尝试过SharedPreferences代码,但没有任何事情发生,f2按钮仍然是GONE和f2lock imageview仍然是可见的
我的意思是这样的:
就像游戏一样,当用户完成等级1时,等级2将解锁
但是在这里,当1级完成时,我按钮是可见的
希望我正确理解了你的问题。如果我没有,请纠正我!
我的看法是,您的用例有几种不同的解决方案:
第一个是正确的SharedPreferences实现,直接构建在现有代码之上。在我看来,这不是最好的方法,因为它滥用了 SharedPreferences 的要点。
另一种方法是在不同的活动中实现回调,但是当您添加更多级别时,这会变得乏味。
我的解决方案是使用一个具有静态值的不同类,它将存储玩家的进度。如果您希望在会话之间保留进度,也可以将其发展为写入磁盘的文件。
您只需要在需要时使用简单的接口函数(例如 )检查玩家的进度getPlayerProgress(),该函数会返回一个整数,解释所达到的最大级别。这还假设您使用单独的函数处理界面,该函数将在每个级别/游戏开始等开始时调用。updateLevel()例如,该函数的名称为。你能理解这个吗?
这是我提到的两个类的示例实现:
/**
* A static class, which handles all player progress, throughout the lifespan of the app.
*
*/
static class PlayerProgress {
// We set progress to static, so it would be alive, no matter what activity you're in.
private static int progress = 1;
/**
* Update the player's progress.
* @param levelNumber: latest level number.
*/
public static void updateProgress(int levelNumber) {
progress = levelNumber;
}
/**
* Get the player's progress.
* @return the latest level number.
*/
public static int getPlayerProgress() {
return progress;
}
}
/**
* The gui handler would need to be called, every time you need to update the screen to the
* appropriate level and it's assets. (Buttons, activities ect.)
*
* I would implement a MainActivity, which would handle the different screens.
*
*/
class guiHandler {
public void updateLevel() {
int progress = PlayerProgress.getPlayerProgress();
/*
* Add your
*/
switch(progress) {
case 1:
/*
* Add the code, which would display level one.
* This would include all button visibilities and maybe load level resources ect.
*/
break;
case 2:
/*
* Same as above.
*/
break;
// You can expand this to as many levels you'd like.
}
}
}
Run Code Online (Sandbox Code Playgroud)
再次,如果我理解有误,请纠正我。如果您想要示例代码,请务必询问。
我希望你有一个愉快的一天。
| 归档时间: |
|
| 查看次数: |
256 次 |
| 最近记录: |