我正在尝试创建一个对话框,在用户注销之前向用户显示倒计时。超时是从另一个活动设置的。
我写了以下代码:
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.os.Bundle;
public class SessionInactivityDialog extends DialogFragment {
public void setInactivityTimeout(long timeout) {
Resources res = getActivity().getResources();
String text = String.format(res.getString(R.string.iminent_logout_text), (timeout / 1000));
((AlertDialog)getDialog()).setMessage(text);
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.iminent_logout);
builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
SessionActivity activity = (SessionActivity)getActivity();
activity.resetTimer();
}
});
return builder.create();
}
}
Run Code Online (Sandbox Code Playgroud)
使用以下几行调用该对话框:
private void showIminentLogoutDialog(long timeout) {
mInactivityDialog.show(getFragmentManager(), …Run Code Online (Sandbox Code Playgroud) android nullpointerexception android-alertdialog dialogfragment
我正在尝试一个简单的应用程序,用户在其中输入他的号码。我会显示一个警报对话框来确认他的手机号码。如果用户选择“是”,我将把他重定向到下一个活动。但问题是,如果用户选择“否”,我想显示相同的编辑号码的活动。
现在我通过调用同一活动的意图来做到这一点。但这是一个不好的做法。如何在活动中显示与上次输入的号码相同的活动?
这是我尝试的方法。按下按钮时
phoneNum = (EditText) findViewById(R.id.editTextPhoneNumber);
String phNum = phoneNum.getText().toString();
AlertDialog.Builder alertDialog = new AlertDialog.Builder(
MainActivity.this);
alertDialog.setTitle("CONFIRM");
alertDialog.setMessage("Is this your correct number? \n" + phNum
+ " a SMS will be sent to verify your phone number.");
alertDialog.setPositiveButton("YES",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
sendOtp();
Intent myIntent = new Intent(getApplicationContext(),
Activity2Activity.class);
startActivity(myIntent);
}
});
alertDialog.setNegativeButton("NO",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
// dialogInterface.cancel();
Intent sameIntent = new Intent(MainActivity.this,
MainActivity.class); …Run Code Online (Sandbox Code Playgroud) 我让数组通过警报对话框选择多个选项 如果数组已满,我想禁用选择
我已经能够在达到限制后停止向数组添加项目,但无法禁用复选框
AlertDialog.Builder builder=new AlertDialog.Builder(this);
View view=inflater.inflate(R.layout.dialog_genre,null,false);
builder.setView(view).setTitle("Select Genres");
builder.setMultiChoiceItems(array, checkedGenres, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if(isChecked) {
if (!selectedgenre.contains(String.valueOf(which)))
if(selectedgenre.size()<5)
{
selectedgenre.add(String.valueOf(which));
checkedGenres[which]=true;
}
else{
Toast.makeText(getApplicationContext(),"you can't add more genres",Toast.LENGTH_LONG).show();
}
}
else if (selectedgenre.contains(String.valueOf(which)))
{
selectedgenre.remove(String.valueOf(which));
checkedGenres[which]=false;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试构建一个简单的 DialogFragment,其中包含一个文本视图和两个按钮(发送和取消)。
我想在 textview 第一个为空时禁用该按钮,但我的代码中的 positiveButton 变量始终为空,并且出现以下异常:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setEnabled(boolean)' on a null object reference
Run Code Online (Sandbox Code Playgroud)
这是代码:
public class SendFriendRequestFragment extends DialogFragment {
private TextView tvEmail = null;
Button positiveButton = null;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
final LayoutInflater inflater = getActivity().getLayoutInflater();
final View layout = inflater.inflate(R.layout.fragment_send_friend_request, null);
// Inflate and set the layout for the …Run Code Online (Sandbox Code Playgroud) AlertDialog 中 RecyclerView 的图像
如图所示,我的 AlertDialog 中有一个 RecyclerView。我的问题是,如果我单击该 RecyclerView 中的按钮(检查按钮,此按钮在 RecyclerViewAdapter 中实例化),如何关闭另一个视图中的对话框?
新来的颤振。我知道如何设置警报对话框的状态,但是由于需要点击 ()=> _createPlayer 之类的功能,它不想重建警报对话框。我想知道如何在需要点击警报对话框时设置状态。
File _image;
GestureDetector(
onTap: () => _createPlayer(),
Run Code Online (Sandbox Code Playgroud)
点击后,它将显示一个警告对话框,如下所示:
_createPlayer() {
return showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(32.0))),
content: Container(
height: 400,
width: 300,
child: Column(
children: <Widget>[
Text('Create Player', style: Theme
.of(context)
.textTheme
.body1),
GestureDetector(
onTap: _getImageCamera,
child: CircleAvatar(
radius: 100,
backgroundColor: Colors.white,
backgroundImage: _image != null ? FileImage(_image) : AssetImage('assets/images/undercover.png'),
),
),
],
),
),
);
});
}
_getImageCamera() async{
var image = await ImagePicker.pickImage(source: ImageSource.camera);
setState(() { …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用可能在 stackoverflow 中为此问题定义的所有解决方案来更改 AlertDialog 的颜色,但它仍然不起作用。
这是我用来尝试实现的代码:
val vacationDialog = AlertDialog.Builder(fragment.context,R.style.DialogTheme)
val factory = LayoutInflater.from(OtrosDetailFragment.fragment.context);
var view = factory.inflate(R.layout.sample, null)
[...]
vacationDialog.setView(view)
val window = vacationDialog.create()
vacationDialog.setPositiveButton("Cerrar"){dialog, which ->
dialog.dismiss()
}
/**Listener called when the AlertDialog is shown**/
vacationDialog.show()
window.setOnShowListener {
/**Get the positive button from the AlertDialog**/
val positiveButton = window.getButton(DialogInterface.BUTTON_POSITIVE)
/**Set your color to the positive button**/
positiveButton.setBackgroundColor(ContextCompat.getColor(fragment.context!!, R.color.colorPrimary))
positiveButton.setTextColor(ContextCompat.getColor(fragment.context!!, R.color.colorPrimary))
positiveButton.setHintTextColor(ContextCompat.getColor(fragment.context!!, R.color.colorPrimary))
positiveButton.setLinkTextColor(ContextCompat.getColor(fragment.context!!, R.color.colorPrimary))
}
Run Code Online (Sandbox Code Playgroud)
这是styles.xml 中定义的DialogTheme 样式,顺便说一下,我找不到更改默认按钮颜色(黑色)的方法,并且似乎覆盖了从代码中尝试的任何更改。
<style name="DialogTheme" parent="android:Theme.Holo">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<!-- No backgrounds, …Run Code Online (Sandbox Code Playgroud) android android-appcompat android-alertdialog material-design material-components-android
我的应用程序在创建其页面之一时通过 AlertDialog 小部件显示欢迎消息。我想在 4 秒后自动关闭对话框,或者让用户通过点击对话框外来关闭它。
void _showWelcomeDialog(BuildContext context) {
final displayName = UserPageInherited.of(context).displayName;
String bodyContent = "Welcome $displayName";
var dialog = WelcomeAlertWidget(bodyContent);
showDialog(
context: context,
builder: (context) {
return dialog;
});
}
Run Code Online (Sandbox Code Playgroud)
welcomeAlertWidget 是一个 StatelessWidget,它返回一个 AlertDialog。
一旦 bloc 进入特定状态,我就会_showWelcomeDialog()在 a 中调用该函数BlocBuilder,如下所示:
WidgetsBinding.instance
.addPostFrameCallback((_) => _showWelcomeDialog(context));
Future.delayed(const Duration(seconds: 4), () {
setState(() {
// Here you can write your code for open new view
Navigator.of(context, rootNavigator: true).pop('dialog');
});
});
Run Code Online (Sandbox Code Playgroud)
如果用户在 4 秒结束之前关闭对话框,并且 Future 返回,背景小部件就会消失,我会留下黑屏。我想知道是否有人可以帮助我避免这个问题。
谢谢
我正在处理一个 Flutter 项目,我试图实现一个类似于MaterialDesign Guidelines 上指定的选项对话框的 AlertDialog ,但底部有一个 TextInput。
我设法得到了与我想要的类似的东西,但我有一个无法解决的问题:当用户点击 TextInput 并出现键盘时,我希望 TextInput 位于键盘顶部,列表视图得到在 y 轴上更小(这就是为什么我只在 ConstrainedBox 上设置 maxHeight),以便用户可以看到他的文本,但我得到的正好相反,列表视图保持相同的大小,而 InputText 不可见.

我曾尝试使用嵌套在 SingleChildScrollView 上的列更改列表视图,或将整个原始列包装在 SingleChildScrollView 上,但它们似乎都不起作用。这是我当前的代码:
@override
Widget build(BuildContext context) {
return AlertDialog(
title: Text(widget.title),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(20))
),
actions: <Widget>[
FlatButton(
child: const Text('CANCEL'),
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
textColor: Theme.of(context).accentColor,
onPressed: () {
widget.onCancel();
},
),
FlatButton(
child: const Text('OK'),
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
textColor: Theme.of(context).accentColor,
onPressed: () {
widget.onOk();
},
),
],
content: Container(
width: double.maxFinite,
child: Column( …Run Code Online (Sandbox Code Playgroud) 我已按如下方式设置我的应用程序:
构建.gradle
implementation 'com.google.android.material:material:1.1.0'
样式文件
<style name="AlertDialogTheme" parent="ThemeOverlay.MaterialComponents.Dialog.Alert">
<item name="buttonBarNegativeButtonStyle">@style/NegativeButtonStyle</item>
<item name="buttonBarPositiveButtonStyle">@style/PositiveButtonStyle</item>
</style>
<style name="NegativeButtonStyle" parent="Widget.MaterialComponents.Button.TextButton.Dialog">
<item name="android:color">@color/colorPrimary</item>
</style>
<style name="PositiveButtonStyle" parent="Widget.MaterialComponents.Button.TextButton.Dialog">
<item name="android:color">@color/colorPrimary</item>
</style>
Run Code Online (Sandbox Code Playgroud)
CheckoutFragment.kt
private fun createConfirmOrderDialog() {
val builder = AlertDialog.Builder(requireContext(), R.style.AlertDialogTheme)
builder.setTitle(getString(R.string.confirm_order))
.setMessage(dialogPrompt)
.setPositiveButton(R.string.confirm) { dialog, _ ->
viewModel.placeOrder()
dialog.dismiss()
}
.setNegativeButton(R.string.cancel) { dialog, _ ->
dialog.dismiss()
}
builder.show()
}
Run Code Online (Sandbox Code Playgroud)
颜色文件
<color name="colorAccent">#F1CB1A</color> // I have this added to the base theme
但是,此设置显示了一个对话框,其中按钮文本不可见,因为文本和背景都是白色的。
我怎样才能解决这个问题?
android android-theme android-alertdialog android-ktx material-components-android
android ×8
flutter ×3
material-components-android ×2
android-ktx ×1
dart ×1
dialog ×1
listview ×1
navigator ×1