保存在 AlertDialog 中选择的值
如何保存选定的值?
private final CharSequence[] mColors = {"1", "2", "3", "4", "5" , "6"};
switch (id) {
case w_COLOR:
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Тест");
builder.setSingleChoiceItems(mColors, 0, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
mResult = item;
}
});
builder.setPositiveButton("Ок", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Toast.makeText(getApplicationContext(), "ITEM: " + mColors[mResult], Toast.LENGTH_LONG).show();
if (mResult == 0)
{
Parameters params = camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_ON);
camera.setParameters(params);
camera.autoFocus(autoFocusCallback);
}
if (mResult == 1)
{
Parameters params = camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(params);
camera.autoFocus(autoFocusCallback);
}
if (mResult == 2)
{
Parameters params = camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_ON);
camera.setParameters(params);
}
if (mResult == 3)
{
Parameters params = camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(params);
}
if (mResult == 4)
{
Parameters params = camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(params);
camera.autoFocus(autoFocusCallback);
}
if (mResult == 5)
{
Parameters params = camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(params);
}
How do I save the selected value?
private final CharSequence[] mColors = {"1", "2", "3", "4", "5" , "6"};
switch (id) {
case w_COLOR:
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Тест");
builder.setSingleChoiceItems(mColors, 0, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
mResult = item;
}
});
builder.setPositiveButton("Ок", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Toast.makeText(getApplicationContext(), "ITEM: " + mColors[mResult], Toast.LENGTH_LONG).show();
if (mResult == 0)
{
Parameters params = camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_ON);
camera.setParameters(params);
camera.autoFocus(autoFocusCallback);
}
if (mResult == 1)
{
Parameters params = camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(params);
camera.autoFocus(autoFocusCallback);
}
if (mResult == 2)
{
Parameters params = camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_ON);
camera.setParameters(params);
}
if (mResult == 3)
{
Parameters params = camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(params);
}
if (mResult == 4)
{
Parameters params = camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(params);
camera.autoFocus(autoFocusCallback);
}
if (mResult == 5)
{
Parameters params = camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(params);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
设置一个变量。例如,我在此调用中翻转布尔值:
此外,您可能应该将
if
结构切换为if...else if... else
结构或 <像上面那样,代码> switch (case) 。现在,如果单击 setPositiveButton,您将检查每个 if 语句,并且它们都是互斥的。您也可以将Parameters params =camera.getParameters();
拉到 if 结构之外,因为它似乎每次都会被调用。Set a variable. For example I flip a boolean value in this call:
Also, you should probably either switch the
if
structure to anif...else if... else
structure orswitch (case)
like you do above. As it is now if that setPositiveButton is clicked you're checking each if statement and they're all mutually exclusive. You can also probably pull theParameters params = camera.getParameters();
outside of the if structure since it seems to be called each time.