Android (w Andengine),创建对话框。从搜索栏获取用户数据。将其保存到共享首选项
Android (w Andengine), Creating a dialog Box. Getting User data from Seek Bar. Saving it to Shared Preferences
基本上我正在开发一个游戏,并希望在用户 click/touches 两个文本(MFX 和 SFX Volumes)时创建一个对话框。我不知道它是否可以用 Andengine 制作,但无论如何我在尝试理解 Google 警报对话框、SeekBar 等开发人员指南后尝试制作它。它如下所示。
早些时候我遇到了一个问题:
E/AndroidRuntime(1878): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
这就是为什么我在更新线程上到达 运行 但运气不好,他的错误仍然存在。我在 android 这方面的工作经验非常少。太初级了还请见谅
private void createDialog(final int pid) {
rM.engine.runOnUpdateThread(new Runnable() {
@Override
public void run() {
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
SeekBar sb = new SeekBar(activity);
final SharedPreferences volumePref = activity.getSharedPreferences(pid + "", Context.MODE_PRIVATE);
final SharedPreferences.Editor editor = volumePref.edit();
sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
int volume = volumePref.getInt(pid + "", 100);// concatenation for conversion of int to string, a short method i like
@Override
public void onStopTrackingTouch(SeekBar arg0) {
switch (pid) {
case MENU_MFX://These are id's which denote which volume we want to change
case MENU_SFX:
editor.putFloat(pid + "", volume);
editor.commit();
break;
}
}
@Override
public void onStartTrackingTouch(SeekBar arg0) {
}
@Override
public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
volume = arg1;
}
});
builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
rM.setMfxVolume(volumePref.getFloat(MENU_MFX + "", 1f));//These methods change volume, these work fine.
rM.setSfxVolume(volumePref.getFloat(MENU_SFX + "", 1f));
}
});
builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
AlertDialog dialog = builder.create();
}
});
}
众所周知,更新线程有时 运行 会产生问题。所以应该这样做:
this.runOnUiThread(new Runnable() {
@Override
public void run() {
MyActivity.this.showDialog(MY_DIALOG_ID);
}
});
如@Nicolas 所说 Here。
基本上我正在开发一个游戏,并希望在用户 click/touches 两个文本(MFX 和 SFX Volumes)时创建一个对话框。我不知道它是否可以用 Andengine 制作,但无论如何我在尝试理解 Google 警报对话框、SeekBar 等开发人员指南后尝试制作它。它如下所示。
早些时候我遇到了一个问题:
E/AndroidRuntime(1878): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
这就是为什么我在更新线程上到达 运行 但运气不好,他的错误仍然存在。我在 android 这方面的工作经验非常少。太初级了还请见谅
private void createDialog(final int pid) {
rM.engine.runOnUpdateThread(new Runnable() {
@Override
public void run() {
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
SeekBar sb = new SeekBar(activity);
final SharedPreferences volumePref = activity.getSharedPreferences(pid + "", Context.MODE_PRIVATE);
final SharedPreferences.Editor editor = volumePref.edit();
sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
int volume = volumePref.getInt(pid + "", 100);// concatenation for conversion of int to string, a short method i like
@Override
public void onStopTrackingTouch(SeekBar arg0) {
switch (pid) {
case MENU_MFX://These are id's which denote which volume we want to change
case MENU_SFX:
editor.putFloat(pid + "", volume);
editor.commit();
break;
}
}
@Override
public void onStartTrackingTouch(SeekBar arg0) {
}
@Override
public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
volume = arg1;
}
});
builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
rM.setMfxVolume(volumePref.getFloat(MENU_MFX + "", 1f));//These methods change volume, these work fine.
rM.setSfxVolume(volumePref.getFloat(MENU_SFX + "", 1f));
}
});
builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
AlertDialog dialog = builder.create();
}
});
}
众所周知,更新线程有时 运行 会产生问题。所以应该这样做:
this.runOnUiThread(new Runnable() {
@Override
public void run() {
MyActivity.this.showDialog(MY_DIALOG_ID);
}
});
如@Nicolas 所说 Here。