Java 将StaticClass设置为使用GUI处理程序将当前按钮集保存为可见
我对我朋友提供的StaticClass代码感到困惑,该代码使用GUI处理程序除了共享首选项之外,还可以进行其他保存 在我的Java 将StaticClass设置为使用GUI处理程序将当前按钮集保存为可见,java,android,user-interface,switch-statement,static-class,Java,Android,User Interface,Switch Statement,Static Class,我对我朋友提供的StaticClass代码感到困惑,该代码使用GUI处理程序除了共享首选项之外,还可以进行其他保存 在我的selectlevel.class public class selectlevel extends Activity { Button f1, f2, f3; ImageView f2lock, f3lock; @Override protected void onCreate(Bundle savedInstanceState) {
selectlevel.class
public class selectlevel extends Activity {
Button f1, f2, f3;
ImageView f2lock, f3lock;
@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.selectlevel);
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");
startActivity (level1);
}
});
}
f2=(Button)findViewById(R.id.f2);
f2lock=(ImageView)findViewById(R.id.f2lock);
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");
startActivity (level2);
}
});
}
}
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
selectlevel.PlayerProgress.updateProgress(1);
finish();
因此,我的朋友使用GUI处理程序提供StaticClass代码,使按钮在最新级别finish()时可见编码>并保存它
这是下面的代码
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:
f2.setVisibility(View.VISIBLE);
f2lock.setVisibility(View.GONE);
break;
case 2:
f3.setVisibility(View.VISIBLE);
f3lock.setVisibility(View.GONE);
break;
// You can expand this to as many levels you'd like.
}
}
}
我只是不知道如何将我的代码和我朋友提供的StaticClass代码结合起来,以便在最新级别完成时保存按钮可见性
有人能帮我写代码吗
更新
我对代码做了如下更改
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");
startActivity (level1);
}
});
f2=(Button)findViewById(R.id.f2);
f2lock=(ImageView)findViewById(R.id.f2lock);
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");
startActivity (level2);
}
});
updateLevels();
}
static class PlayerProgress {
private static int progress = 0;
public static void updateProgress(int levelNumber) {
progress = levelNumber;
}
public static int getPlayerProgress() {
return progress;
}
}
public void updateLevels() {
int progress = PlayerProgress.getPlayerProgress();
switch(progress) {
case 1:
f2.setVisibility(View.VISIBLE);
f2lock.setVisibility(View.GONE);
break;
case 2:
break;
// You can expand this to as many levels you'd like.
}
}
并在我的levelone.class
public class selectlevel extends Activity {
Button f1, f2, f3;
ImageView f2lock, f3lock;
@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.selectlevel);
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");
startActivity (level1);
}
});
}
f2=(Button)findViewById(R.id.f2);
f2lock=(ImageView)findViewById(R.id.f2lock);
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");
startActivity (level2);
}
});
}
}
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
selectlevel.PlayerProgress.updateProgress(1);
finish();
当levelone.class
finishF2按钮仍然不在时,如何修复此问题 因此,您可以使用此代码
选择level.java
package com.example.myapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
public class selectlevel extends Activity {
Button f1, f2, f1lock, 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.selectlevel);
f1 = (Button) findViewById(R.id.f1);
f1lock = (Button) findViewById(R.id.f1lock);
f1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent level1 = new Intent();
level1.setClassName("com.example.myapp", "com.example.myapp.levelone");
startActivity(level1);
}
});
f2 = (Button) findViewById(R.id.f2);
f2lock = (Button) findViewById(R.id.f2lock);
f2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent level2 = new Intent();
level2.setClassName("com.example.myapp", "com.example.myapp.leveltwo");
startActivity(level2);
}
});
updateLevels();
}
static class PlayerProgress {
// We set progress to static, so it would be alive, no matter what activity you're in.
private static int progress = 0;
/**
* 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;
}
}
public void updateLevels() {
int progress = PlayerProgress.getPlayerProgress();
switch (progress) {
case 0:
f1.setVisibility(View.VISIBLE);
f1lock.setVisibility(View.GONE);
f2.setVisibility(View.GONE);
f2lock.setVisibility(View.VISIBLE);
break;
default:
f1.setVisibility(View.VISIBLE);
f1lock.setVisibility(View.GONE);
f2.setVisibility(View.VISIBLE);
f2lock.setVisibility(View.GONE);
break;
}
}
}
package com.example.myapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class levelone extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.levelone);
Button finishf1 = (Button) findViewById(R.id.finishf1);
finishf1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
selectlevel.PlayerProgress.updateProgress(1);
startActivity(new Intent(levelone.this, selectlevel.class));
}
});
}
}
selectlevel.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:layout_width="200dp"
android:layout_height="100dp"
android:id="@+id/f1"
android:text="Level 1"/>
<Button
android:layout_width="200dp"
android:layout_height="100dp"
android:id="@+id/f1lock"
android:text="Level 1 disabled"/>
<Button
android:layout_width="200dp"
android:layout_height="100dp"
android:id="@+id/f2"
android:text="Level 2"/>
<Button
android:layout_width="200dp"
android:layout_height="100dp"
android:id="@+id/f2lock"
android:text="Level 2 disabled"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:layout_width="200dp"
android:layout_height="100dp"
android:id="@+id/finishf1"
android:text="Finish Level 1"/>
</LinearLayout>
levelone.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:layout_width="200dp"
android:layout_height="100dp"
android:id="@+id/f1"
android:text="Level 1"/>
<Button
android:layout_width="200dp"
android:layout_height="100dp"
android:id="@+id/f1lock"
android:text="Level 1 disabled"/>
<Button
android:layout_width="200dp"
android:layout_height="100dp"
android:id="@+id/f2"
android:text="Level 2"/>
<Button
android:layout_width="200dp"
android:layout_height="100dp"
android:id="@+id/f2lock"
android:text="Level 2 disabled"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:layout_width="200dp"
android:layout_height="100dp"
android:id="@+id/finishf1"
android:text="Finish Level 1"/>
</LinearLayout>
code-bro中仍然存在一个问题,我将更新我的问题我将更新我的帖子并添加我的代码,这很好。希望它能帮助你)