我的应用程序中有 2 个编辑文本,1 个按钮用于在编辑文本中添加输入数字,1 个文本视图用于显示结果。如果单击按钮时我的编辑文本选项卡为空或为空,我想添加一条祝酒消息。我已经搜索并尝试了所有解决方案......但似乎没有任何效果。请帮助!!
这是我将两个数字相加的代码。
package com.example.mycalc;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText editText1 = (EditText)findViewById(R.id.editText1);
final EditText editText2 = (EditText)findViewById(R.id.editText2);
Button button1 = (Button)findViewById(R.id.button1);
final TextView textView1 = (TextView)findViewById(R.id.textView1);
textView1.setText(" ");
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
double edit1 = Double.valueOf(editText1.getText().toString());
double edit2 = Double.valueOf(editText2.getText().toString());
double …Run Code Online (Sandbox Code Playgroud) 我从 MainActivity 触发 AlertDialog,它工作正常,如下所示:
public void showCustomAlert(String text){
final String alertText = text;
runOnUiThread(new Runnable() {
@Override
public void run() {
AlertDialog.Builder myDialogBox = new AlertDialog.Builder(mContext);
myDialogBox.setTitle("Alert");
myDialogBox.setMessage(alertText);
myDialogBox.setCancelable(false);
myDialogBox.setPositiveButton("OK", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
}
});
myDialogBox.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog alertDialog = myDialogBox.create();
alertDialog.show();
}
});
}
Run Code Online (Sandbox Code Playgroud)
当我在 MainActivity 之上打开另一个活动并再次触发 AlertBox 时,问题就出现了:它位于该活动的后面。当我关闭活动时,它就在那里:AlertDialog 正在显示。
如何让这个 AlertDialog 始终显示在顶部?
注意:此 AlertDialog 是由我的 MainActivity 上的推送通知侦听器触发的,而不是通过单击侦听器触发。
我有一种在片段中创建警报对话框的方法
private void alertDialog() {
final EditText editTextField = new EditText(this.getContext());
AlertDialog dialog = new AlertDialog.Builder()
.setTitle("Title")
.setMessage("Message")
.setView(editTextField)
.setPositiveButton("OK", this)
.setNegativeButton("Cancel", null)
.create();
dialog.show();
}
Run Code Online (Sandbox Code Playgroud)
然后我实施了 DialogInterface.OnClickListener
public void onClick(DialogInterface dialogInterface, int i){
switch(i){
case DialogInterface.BUTTON_POSITIVE:
String name = String.valueOf(editTextField.getText());
break;
case DialogInterface.BUTTON_NEGATIVE:
break;
}
}
Run Code Online (Sandbox Code Playgroud)
但是editTextFieldinonClick方法无法引用警报对话框。有没有办法解决这个问题?或者唯一的方法是创建一个带有编辑文本的布局并setView进入警报对话框构建器?
我有AlertDialog,还有一些按钮。通过单击它们,一些函数/方法开始工作。
但是如果用户在 AlertDialog 视图之外点击怎么办?
我想启动特定功能,然后用户单击空白字段(对话框视图之外)
如何在 AlertDialog.Only AlertDialog 中显示项目,没有任何项目我正在尝试下面的代码,但这似乎不起作用:
CharSequence[] choices = {"Choice1", "Choice2", "Choice3"};
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context)
.setTitle(context.getString(R.string.title))
.setPositiveButton(context.getString(R.string.Ok), null)
.setNeutralButton(context.getString(R.string.Cancel), null)
.setSingleChoiceItems(choices, 1, null);
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
Run Code Online (Sandbox Code Playgroud)
我正在使用材料组件样式:
<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
Run Code Online (Sandbox Code Playgroud)
android android-alertdialog material-design material-components material-components-android
我正在使用MaterialAlertDialogBuilder,因为我想要圆角和白天和夜间模式。请注意,我不能使用dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));线条,因为在我的风格中,我在 style.xml 中有不同的父级,我用它根据日/夜模式设置颜色。
问题:如何更改 MaterialAlertDialogBuilder 中的正/负按钮背景颜色?
请注意,可绘制背景不适用于MaterialAlertDialogBuilder
代码:
public void showNotesDialog() {
MaterialAlertDialogBuilder alertDialogBuilder = new MaterialAlertDialogBuilder(this, R.style.dialogBoxStyle);
LayoutInflater li = LayoutInflater.from(this);
View promptsView = li.inflate(R.layout.row_note_layout, null);
alertDialogBuilder.setView(promptsView);
etNote = promptsView.findViewById(R.id.et_note);
String buttonName = getString(R.string.add);
if (!getNotes.isEmpty()) {
buttonName = getString(R.string.update);
etNote.getText().clear();
etNote.setText(getNotes);
}
alertDialogBuilder.setPositiveButton(buttonName, null);
alertDialogBuilder.setNegativeButton(getString(R.string.cancel), null);
alertDialogBuilder.setNeutralButton("Clear", null);
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.setCancelable(false);
alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
Button buttonPositive = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE);
Button buttonNegative = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_NEGATIVE); …Run Code Online (Sandbox Code Playgroud) 为什么我的警报对话框不适应我手机的暗模式?在我的手机中激活暗模式时,几乎看不到两个按钮的文本,而标题和消息颜色则相反。为什么按钮颜色也不反转?该图标在非深色模式下也不能很好地显示。如何解决所有这些问题?
val builder = AlertDialog.Builder(this)
builder.setTitle(getString(R.string.title))
builder.setMessage(getString(R.string.message))
builder.setPositiveButton(getString(R.string.positive)) { dialog, which ->
Toast.makeText(applicationContext, getString(R.string.pos_toast), Toast.LENGTH_LONG).show()
}
builder.setNegativeButton(getString(R.string.negative)) { dialog, which ->
Toast.makeText(applicationContext, getString(R.string.negative_toast), Toast.LENGTH_LONG).show()
}
builder.setIcon(android.R.drawable.ic_dialog_alert)
builder.show()
Run Code Online (Sandbox Code Playgroud)
我的 stiles.xml:
<resources>
<!-- Base application theme. -->
<style name="MyTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="generalnotitle" parent="MyTheme">
<item name="android:windowNoTitle">true</item>
</style>
</resources>
Run Code Online (Sandbox Code Playgroud) android android-theme android-alertdialog kotlin material-components-android
我是颤振新手,如果用户单击“计算”按钮,我会尝试显示结果的对话框警报。我想将“文本”结果更改为对话框警报,例如“(总天数)的预测是(结果)”任何人都可以帮助我如何做到这一点?我找不到合适的资源,谢谢
class TransactionYearly extends StatefulWidget {
@override
_TransactionYearlyState createState() => _TransactionYearlyState();
}
class _TransactionYearlyState extends State<TransactionYearly> {
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
final TextEditingController amount = new TextEditingController();
final TextEditingController day = new TextEditingController();
double _result;
@override
void initState() {
super.initState();
}
@override
void dispose() {
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(getTranslated(context, 'prediction_calculation'),),
elevation: 0,
brightness: Brightness.light,
backgroundColor: primary,
leading: IconButton(
onPressed: (){
Navigator.pop(context, MaterialPageRoute(builder: (context) => Transactions()));
},
icon: Icon(Icons.arrow_back_ios,
size: 20, …Run Code Online (Sandbox Code Playgroud) 这是我的问题.我想要一个具有标题和正面按钮的AlertDialog.我想在XML文件中描述AlertDialog的内容(标题/按钮除外).我已经dlg_addpwd.xml在我的布局资源中创建了一个文件.这是我正在使用的代码:
AlertDialog alert = new AlertDialog.Builder(this);
alert.setTitle("Password access");
alert.setView(findViewById(R.layout.dlg_addpwd));
alert.setPositiveButton("Add", listenAddPwdDlg);
alert.show();
Run Code Online (Sandbox Code Playgroud)
我想这一行
alert.setView(findViewById(R.layout.dlg_addpwd));
Run Code Online (Sandbox Code Playgroud)
是错的,不是吗?所以我的问题的主要思想是:如何将AlertDialog定义为XML文件中描述的视图?
谢谢
文森特
我在onCreate中启动了一个警告对话框,允许用户从两个不同的活动中进行选择.我使用了具有正值和负值的警告对话框但不幸的是我收到了错误.是否无法使用此类警报对话框来运行两种不同的活动?如果是的话怎么可能?我遇到这种错误:
[Android Application]
[Android Application]
[Android Application]
[Android Application]
[Android Application]
[Android Application]
[Android Application]
[Android Application]
DalvikVM [localhost:8602]
Thread [<1> main] (Running)
Thread [<10> Binder_2] (Running)
Thread [<9> Binder_1] (Running)
Thread [<11> Thread-198] (Suspended (exception RuntimeException))
<VM does not provide monitor information>
Handler.<init>(Handler$Callback, boolean) line: 200
Handler.<init>() line: 114
AlertDialog(Dialog).<init>(Context, int, boolean) line: 109
AlertDialog.<init>(Context, int, boolean) line: 114
AlertDialog$Builder.create() line: 931
MainActivity$splashscreen.run() line: 68
[Android Application]
Run Code Online (Sandbox Code Playgroud)
这是我的警告对话框的代码
public class MainActivity extends Activity {
//ImageView image;
@Override
protected void …Run Code Online (Sandbox Code Playgroud)