gpt4 book ai didi

java - 复选框和 AlertDialog

转载 作者:行者123 更新时间:2023-11-30 03:31:35 25 4
gpt4 key购买 nike

我必须在 java 警报中打开一个对话框,以通知用户如果他想删除广告必须购买应用程序的付费版本...我做了两个 java 文件..另一个叫对话的记忆一下上面的文件和布局 xml 中定义的其他元素...问题是如果我放置新的对话框 (this) 会给我错误...相反,如果当前通过单击复选框运行应用程序会使应用程序崩溃。复选框崩溃后检查应该有...复选框不能可检查。

package com.X.XX.XXX;


import android.app.AlertDialog;
import android.content.Context;

public class Dialogo {

public static final int BUY = 1000;
AlertDialog.Builder builder;

public void mostra(int Id) {
// TODO Auto-generated method stub
switch(Id)
{
case BUY:
builder.setTitle("Title");
builder.setMessage("TEXT ");
builder.setNegativeButton("CLOSE", null);
builder.setPositiveButton("BUY", null);
break;
}
builder.create().show();
}

}

package com.X.XX;

import com.X.XX.XXX.Dialog;

import android.content.Intent;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

public class Settings extends PreferenceActivity{

CheckBoxPreference no_ads;
Dialogo dialogo;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.addPreferencesFromResource(R.xml.settings);
dialogo = new Dialogo();

no_ads = (CheckBoxPreference) this.findPreference("no_ads");
no_ads.setOnPreferenceClickListener(new OnPreferenceClickListener(){

@Override
public boolean onPreferenceClick(Preference preference) {
// TODO Auto-generated method stub
dialogo.mostra(Dialogo.BUY);
((CheckBoxPreference)preference).setChecked(false);
return false;
}});

}

最佳答案

您必须在使用之前初始化构建器变量。因此,请确保将上下文也传递给您的函数,首先像这样声明它:

public void mostra(Context context, int Id) {
builder = new AlertDialog.Builder(context);

然后这样调用它:

dialogo.mostra(Settings.this, Dialogo.BUY);

完整的工作示例:

package com.X.XX.XXX;

import android.app.AlertDialog;
import android.content.Context;

public class Dialogo {
public static final int BUY = 1000;

public void mostra(Context context, int Id) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
// TODO Auto-generated method stub
switch(Id)
{
case BUY:
builder.setTitle("Title");
builder.setMessage("TEXT ");
builder.setNegativeButton("CLOSE", null);
builder.setPositiveButton("BUY", null);
break;
}
builder.create().show();
}
}


package com.X.XX;
import com.X.XX.XXX.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

public class Settings extends PreferenceActivity{
CheckBoxPreference no_ads;
Dialogo dialogo;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.addPreferencesFromResource(R.xml.settings);
dialogo = new Dialogo();

no_ads = (CheckBoxPreference) this.findPreference("no_ads");
no_ads.setOnPreferenceClickListener(new OnPreferenceClickListener(){
@Override
public boolean onPreferenceClick(Preference preference) {
// TODO Auto-generated method stub
dialogo.mostra(Settings.this, Dialogo.BUY);
((CheckBoxPreference)preference).setChecked(false);
return false;
}
});
}
}

关于java - 复选框和 AlertDialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17370866/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com