我试图向Android OnClickListener添加一个是/否确认弹出窗口.可以在setOnClickListener中使用AlertDialog.Builder,还是应该采用不同的方法?我已经使用eclipse/android调试环境逐步完成了以下代码,并期望弹出窗口出现在.create,等待用户响应,但事实并非如此.我是android和java的新手,所以我可能会遗漏一些明显的东西.任何建议,想法或方向将不胜感激.
public class Controller extends Activity {
...
buttonOn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
new AlertDialog.Builder(Controller.this)
.setIcon(R.drawable.ic_menu_help)
.setMessage("Are You Sure?")
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
// Positive response code
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Negative response code
}
})
.create();
}
});
Run Code Online (Sandbox Code Playgroud) 我想显示一个自定义警报对话框,其结构已在XML文件中定义.这是我使用的代码.
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(findViewById(R.layout.inputform));
builder.show();
Run Code Online (Sandbox Code Playgroud)
单击按钮时会触发它.但是,窗口没有显示出来.我在屏幕顶部有一个透明的灰色层,无法点击任何东西.但是,使用setMessage或显示复选框等一些基本代码效果很好.任何人都可以指出我做错了吗?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<TableLayout android:id="@+id/TableLayout01"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<TableRow android:id="@+id/TableRow01" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:text="Room name" android:id="@+id/TextView01"
android:layout_width="fill_parent" android:layout_height="wrap_content"/>
<EditText android:hint="Enter room name" android:id="@+id/EditText01"
android:layout_width="0dp" android:layout_height="wrap_content"
android:layout_weight="1"/>
</TableRow>
<TableRow android:id="@+id/TableRow02" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:text="Point id" android:id="@+id/TextView02"
android:layout_width="fill_parent" android:layout_height="wrap_content"/>
<EditText android:hint="Enter point id" android:id="@+id/EditText02"
android:layout_width="0dp" android:layout_height="wrap_content"
android:layout_weight="1"/>
</TableRow>
<TableRow android:id="@+id/TableRow03" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:text="OK" android:id="@+id/btnOK"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</TableRow>
</TableLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud) 因此,每次收到文本时都会打开一个对话框.如果有一个已打开,我希望它不打开.我试图通过使用isShowing()来检查是否打开了一个但是我继续得到类型AlertDialog.Builder的方法isShowing()未定义.这是坏代码的一部分.现在任何帮助都会如此甜蜜.
public class PopUpReply extends Activity{
AlertDialog.Builder alertbox;
AlertDialog.Builder alert;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// prepare the alert box
alertbox.isShowing();
alertbox = new AlertDialog.Builder(this);
Run Code Online (Sandbox Code Playgroud) 我在Android应用程序上有一个活动,它启动与我的服务器的同步过程.此过程消耗大量内存和处理,并且需要一些时间才能完成.
该过程完成后,将向用户显示AlertDialog,显示该过程是否已成功完成.
如果我离开活动前景,一切都会按预期工作.但是,有时,当我离开应用程序时,后台进程继续其工作,但是,当我返回到应用程序时,它崩溃并出现以下错误:
android.view.WindowManager $ BadTokenException:无法添加窗口 - 令牌android.os.BinderProxy@4086ea48无效; 你的活动在运行吗?
这个错误可能是因为操作系统破坏了活动吗?如果是,那么进程(在另一个线程上运行)如何继续运行?而且,如果我们确认问题是由活动的破坏引起的,我怎么能处理这种情况,避免崩溃并向用户显示对话框?
这是一些代码......
public class ActSincronizacao extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sincronizacao);
//Prepare so starts the process
[...]
// Defines a Handler that will be called when the process gets finished
oSocketEngine.addProgressUpdateFinishListener(new ProgressUpdateFinishListener() {
@Override
public void ProgressUpdateFinishOccurred(final ProgressUpdateFinish evt) {
//Do some processing that doesn´t envolve UI
[...]
// Process that envolves UI
runOnUiThread(new Runnable() {
public void run() {
if ((boolean)evt.getArgs().getResult()) {
//If the process …Run Code Online (Sandbox Code Playgroud) 如何将警报主题设置为标准Android主题之一?我想使用Holo Dark,因为弹出窗口默认为Holo Light。我的代码:
AlertDialog.Builder confirm = new AlertDialog.Builder(this);
confirm.setTitle("Confirm");
confirm.setMessage("Confirm and set delay?");
confirm.setCancelable(false);
confirm.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
startDelay();
}
});
confirm.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
confirm.show();
Run Code Online (Sandbox Code Playgroud) 我想为我的一些活动制作一个"全局"警报对话框.我的意思是我创建了一个扩展的类,Activity它还为警报对话框创建了一个函数,该函数包含两个参数(消息和标题).
public class MyDialogAlert extends Activity {
public void createDialog(String title, String message)
{
new AlertDialog.Builder(this)
.setTitle(title)
.setMessage(message)
.setPositiveButton("yes", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
//(e.g) open another activity
}
})
.setNegativeButton("no", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
//some stuff ..
}
})
.show();
}
Run Code Online (Sandbox Code Playgroud)
}
如何在不同的活动中调用此函数并更改它的负面和正面按钮以执行除上述类中最初定义的操作之外的其他操作?
谢谢您的帮助 :)
我正在尝试自定义DialogFragment的某些样式。
我用以下方式更改了标题的颜色:
TextView title = (TextView)getDialog().findViewById( android.R.id.title );
title.setTextColor( getResources().getColor( R.color.base_app_color ) );
Run Code Online (Sandbox Code Playgroud)
现在,我需要更改标题的背景颜色和底线颜色。
有谁知道哪些是那些android.R.id。或如何存档?

我正在使用AppCompatDialog构建对话框界面。
我的目标是在显示对话框视图时消除阴影?
这是代码示例:
private void showWrongLoginPassDialog(String message){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(getString(R.string.ad_login_error_title));
builder.setMessage(message);
builder.setPositiveButton(getString(R.string.ad_login_error_positive),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
}
Run Code Online (Sandbox Code Playgroud) 我通过以下方式主题:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/primary</item>
<item name="colorPrimaryDark">@color/primaryDark</item>
<item name="colorAccent">@color/accent</item>
<item name="alertDialogTheme">@style/AppTheme.Dialog</item>
</style>
<style name="AppTheme.Dialog" parent="Theme.AppCompat.Dialog.Alert">
<item name="colorAccent">@color/accent</item>
</style>
Run Code Online (Sandbox Code Playgroud)
但是,当我在我的应用程序中创建AlertDialogs时,它们根本不是主题.其他组件如ActionBar正在正确着色.我错过了什么吗?
我使用的是AppCompat版本com.android.support:appcompat-v7:23.1.1,我的设备安装了Android 4.4.4.
我刚刚在我的应用程序中更新了库版本,现在AlertDialog按钮的文本颜色为白色.在此之前,colorAccent如果我没记错的话,那就是指定它们的属性.我尝试了很多不同的属性,但似乎没有一个属性能够工作.
示例照片 - 在右下方,您可以看到非常隐蔽的按钮:
我当前的警报对话框样式:
<style name="AlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="colorAccent">@color/website_main</item>
<item name="android:textColor">@color/website_main</item>
</style>
Run Code Online (Sandbox Code Playgroud)
以我的主题风格:
<item name="android:alertDialogStyle">@style/AlertDialogStyle</item>
Run Code Online (Sandbox Code Playgroud)
我正在使用的支持/设计/ appcompat的当前版本是: 25.1.0
我在其中创建的Java代码AlertDialog:
android.support.v7.app.AlertDialog.Builder alertDialogBuilder = new android.support.v7.app.AlertDialog.Builder(activity);
alertDialogBuilder
.setCancelable(false)
.setPositiveButton(activity.getString(R.string.ok), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
<UnrelatedLogic...>
}
});
return alertDialogBuilder.show();
Run Code Online (Sandbox Code Playgroud)
在这里我使用的ListPreference还使用了AlertDialog:
android.support.v7.preference.ListPreference lang_preference = (android.support.v7.preference.ListPreference) findPreference("language_chooser");
Run Code Online (Sandbox Code Playgroud)
有什么建议?