Android 自定义警报对话框中的 OnClickListener

2024-05-20

我是一个自学成才的初学者,感谢耐心!谢谢!

在 Eclipse 中,我使用自己的 xml 文件(“custom_dialog”)创建了一个自定义警报对话框,称为“usernamealert”。

如果用户尚未输入用户名(即 username.length == 0),我希望弹出警报。

在这个布局中,我有一个 textView(“你叫什么名字?”)、editText 和按钮(“usernameButton”)。

在为按钮添加 onclicklistener 之前,一切正常。这是我的(相关)Java:

LayoutInflater inflater = getLayoutInflater();
View dialoglayout = inflater.inflate(R.layout.custom_dialog, (ViewGroup)     getCurrentFocus());
AlertDialog.Builder usernamebuilder = new AlertDialog.Builder(this);
usernamebuilder.setView(dialoglayout);

AlertDialog usernamealert = usernamebuilder.create();

当我放入 onclicklistener 时,它坏了!我应该把它放在哪里?

(以下是我尝试过的......全部在我的OnCreate中)

LayoutInflater inflater = getLayoutInflater();
View dialoglayout = inflater.inflate(R.layout.custom_dialog, (ViewGroup)getCurrentFocus());
AlertDialog.Builder usernamebuilder = new AlertDialog.Builder(this);
usernamebuilder.setView(dialoglayout);


Button usernameButton = (Button) usernamealert.findViewById(R.id.usernameButton);
usernameButton.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {  
//store username in sharedprefs
usernamealert.dismiss();



}
});

代码之后我说:

if (username.length() == 0) {
            usernamealert.show();
        }

再说一遍,在我开始摆弄按钮之前它就起作用了!


需要指定代码将在哪里搜索按钮,如果它只有“findViewById”,它将在主机的 xml 中搜索,应该是

LayoutInflater inflater =getLayoutInflater();
                View myview = inflater.inflate(R.layout.dialoghireteachernegotiate, null);
                // Inflate and set the layout for the dialog
                // Pass null as the parent view because its going in the dialog layout
                builder.setView(myview);
                 Button addS = (Button) myview.findViewById (R.id.bAddS);

这是我的类 Hireteachernegotiate.class 的一部分,它的布局为 Hireteachernegotiate.xml

 AlertDialog.Builder builder = new AlertDialog.Builder(this);
                // Get the layout inflater
                LayoutInflater inflater =getLayoutInflater();
                View myview = inflater.inflate(R.layout.dialoghireteachernegotiate, null);
                // Inflate and set the layout for the dialog
                // Pass null as the parent view because its going in the dialog layout
                builder.setView(myview);

                Button addS = (Button) myview.findViewById (R.id.bAddS);
                addS.setOnClickListener(new View.OnClickListener() {

                    public void onClick(View v) {
                        //do some stuff
                    }
                });

                Button minusS = (Button) myview.findViewById (R.id.bMinusS);
                addS.setOnClickListener(new View.OnClickListener() {

                    public void onClick(View v) {
                       //do other stuff
                    }
                });

                // Add action buttons
                       builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
                           public void onClick(DialogInterface dialog, int id) {


                               dialog.cancel();
                           }
                       });
                   builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int id) {
                           dialog.cancel();
                       }
                   });   



            builder.show();

这是对话框布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <Button
        android:id="@+id/bAddS"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />


    <Button
        android:id="@+id/bMinusS"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android 自定义警报对话框中的 OnClickListener 的相关文章

  • 如何在android中使用kso​​ap2库解析复杂的响应

    大家好 我正在使用 Ksoap2 库解析以下类型的响应 但没有成功获得结果 我的请求如下
  • 如何从我的班级访问活动 UI?

    我有一个活动创建我的类的对象实例 file MyActivity java public class MyActivity extends Activity TextView myView TextView findViewById R i
  • 仅使用 Context 而不是 Activity 实例显示对话框

    如果我使用 Activity 实例 我可以显示对话框 但当我使用上下文或应用程序上下文实例时 对话框不会显示 AlertDialog Builder builder new AlertDialog Builder activity buil
  • 如何列出我的应用程序以供下载文件?

    我想通过我的应用程序从浏览器下载文件 我正在尝试将我的应用程序列在complete action using对话 它显示其他操作 例如查看文件等 但在下载文件的情况下 它不会显示在对话框中 我怎样才能像图片中那样列出我的应用程序 我在我的活
  • 标准呼叫屏幕上的活动窗口 - 启用按钮

    我想在通话屏幕活动上添加一个小窗口 弹出窗口 谷歌语音 and 世界通话地点和时间 http areacellphone com 2010 04 android worldcallplaceandtime apps know place a
  • Android Studio:无法终止 xxx 的现有进程

    The application is a system application and it will be started by other applications When I install the application from
  • Android 滚动视图无法以编程方式创建。

    我想在我的应用程序中使用滚动视图 我尝试将文本视图添加到滚动视图中 但除了滚动视图的背景颜色之外 我看不到任何渲染的内容 我是这样做的 public class MyView extends ViewGroup ScrollView myS
  • 如何以编程方式在 Android 中查找平板电脑或手机?

    我的情况是 手机和平板电脑的逻辑是相同的 但布局上略有不同 我尝试使用以下代码 public static boolean findoutDeviceType Context context return context getResour
  • 使用 Gradle 进行 Travis-CI Android 测试总是超时

    我正在尝试在 Travis CI 上构建和测试我的项目 它每次都会在我的所有存储库上向我显示相同的重复输出 这是我的 travis ymlhttps github com carts uiet cartsbusboarding blob m
  • 在 Marshmallow 中获取蓝牙本地 mac 地址

    在 Marshmallow 之前 我的应用程序将通过以下方式获取其设备 MAC 地址BluetoothAdapter getDefaultAdapter getAddress 现在 随着 Marshmallow Android 的回归02
  • 动态选取框文本

    是否可以将列表视图的 java 编码中的文本添加到 Android 中的选取框滚动中 如果可以 请告诉我如何做 如果需要 我将发布使用的代码 这是列表视图使用的 XML 如下
  • Android NDK C++“wstring”支持

    我有用 C 编写的源代码 lib 现在我想在 Android NDK 项目 NDK 6 中编译并使用相同的源代码 lib 我能够编译大多数 C 文件 除了基于 std wstring 的功能 在 Application mk 中 当我指定时
  • 检查应用程序是否首次运行[重复]

    这个问题在这里已经有答案了 我是 Android 开发新手 我想根据应用程序安装后首次运行来设置一些应用程序的属性 有什么方法可以发现应用程序是第一次运行 然后设置其首次运行属性吗 下面是一个使用的例子SharedPreferences实现
  • 如何使用云打印打印Android活动显示

    我正在尝试将 Google 云打印实现到应用程序中 遵循集成指南 https developers google com cloud print docs android 我试图通过打印 google com 来保持基本 单击我创建的打印按
  • 使用 qbs 构建 qt 应用程序

    我想知道在 Linux 上使用 qbs 编译 构建和创建 Android Qt 应用程序的步骤 我拥有所有必要的工具 目前我可以使用 qmake 创建 apk Qbs 目前不支持构建 Qt Android 应用程序 Qbs v1 4 中引入
  • 停止前台运行的服务的正确方法是什么

    我正在尝试停止作为前台服务运行的服务 当前的问题是当我打电话时stopService 通知仍然保留 因此 在我的解决方案中 我添加了一个接收器 我正在将其注册到onCreate 在 的里面onReceive 我调用的方法stopforegr
  • 如何将QR码中的3个方块替换为圆圈以使用Paint android使用zxing自定义QR码?

    我用它作为自定义的参考 从方形到圆形使用zxing生成的QR码它是在java中所以我尝试将它转换为在android中使用 使用 zxing 生成具有自定义点形状的 QR 码 https stackoverflow com questions
  • 无法通过usb调试安装android应用程序

    我知道这是一个老问题 但我第一次尝试在 Redmi note 4 上安装我的应用程序 每当我尝试安装时 它都会显示一个对话框 安装失败 并显示消息无法建立会话 通过卸载现有版本的 apk 如果存在 然后重新安装 可能可以解决此问题 警告 卸
  • 如何检测用户是否禁用 GPS(Android - Play 服务)

    我使用 gms location LocationListener Google Play 服务 来获取用户的位置 它工作正常 但我想检测用户何时禁用或启用他 她的 GPS 就像这张照片一样 当我打开 关闭位置时 不会调用任何方法 当我切换
  • Libgdx 和 Google 应用内购买结果

    我遵循了这些指示 https github com libgdx libgdx wiki Interfacing with platform specific code使用 ActionResolver 接口集成 Libgdx 和原生 An

随机推荐

  • 打字稿 - 字符串'不可分配给类型'FC

    我收到以下错误 Type props PropsWithChildren lt amount number gt gt string is not assignable to type FC lt amount number gt Type
  • 字典和数组作为类变量与实例变量

    这是赚取积分的简单方法 请解释以下内容 class C a b 0 c def init self self x def d self k v self x k v self a k v self b v self c append v d
  • 按字母顺序过滤 Firestore 数据以对 Google Cloud 中的文档读取进行分类/减少

    基于这样的事实Cloud Firestore 不支持全文搜索 https firebase google com docs firestore solutions search到目前为止 我决定问这个question https stack
  • 停止 SAS 执行

    快速提问 是否有一种单行 或相当短的 方法可以从窗口环境中取消进一步 SAS 语句的执行 这些是methods http www2 sas com proceedings sugi31 063 31 pdf我知道 但它们会让人厌烦 尤其是在
  • 是否可以有一个 out ParameterExpression?

    我想定义一个 Lambda 表达式out范围 有可能做到吗 下面是我尝试过的 C Net 4 0 控制台应用程序的代码片段 正如您在 procedure25 中看到的 我可以使用 lambda 表达式来定义具有输出参数的委托 但是 当我想使
  • 耐用功能是否适合大量活动?

    我有一个场景 需要计算 500k 活动 都是小算盘 由于限制 我只能同时计算 30 个 想象一下下面的简单示例 FunctionName Crawl public static async Task
  • 当前的 x86 架构是否支持非临时加载(来自“正常”内存)?

    我知道有关此主题的多个问题 但是 我没有看到任何明确的答案或任何基准测量 因此 我创建了一个处理两个整数数组的简单程序 第一个数组a非常大 64 MB 第二个数组b很小 无法放入 L1 缓存 程序迭代a并将其元素添加到相应的元素中b在模块化
  • 获取 VS Code 扩展中的当前突出显示

    我不是在谈论当前的选择 可以通过以下方式访问它vscode window activeTextEditor selection 当光标位于标识符 变量名称等内部时 它会突出显示 如以下屏幕截图所示 这个突出显示的对象叫什么 我如何访问它 搜
  • 访问 maven-release-plugin 的发布版本

    我在 Jenkins 作业中使用 maven release plugin 并且在执行 shell 脚本时启用了 在 SCM 运行之前运行构建步骤 我想访问此 shell 脚本中的发布版本 但无法访问它 我不确定这个插件中存储发布版本的变量
  • Redux-saga 从操作中获取数据返回patternOrChannel 未定义

    我需要将动态数据从屏幕发送到操作 减速器 并使用该数据从 API 获取数据 但是当我在我的rootSaga我会收到这样的错误 在检查 take patternOrChannel 时未捕获 patternOrChannel 未定义未捕获在 r
  • 如何读取Python字节码?

    我很难理解 Python 的字节码及其dis module import dis def func x 1 dis dis func 上述代码在解释器中输入时会产生以下输出 0 LOAD CONST 1 1 3 STORE FAST 0 x
  • Elastic Beanstalk 中的 enum34 问题

    我正在尝试在 Elastic Beanstalk 中设置 django 环境 当我尝试通过requirements txt 文件安装时 我遇到了python3 6 问题 File opt python run venv bin pip li
  • 检查字典键是否有空值

    我有以下字典 dict1 city name yass region zipcode phone address tehsil planet mars 我正在尝试创建一个基于 dict1 的新字典 但是 它不会包含带有空字符串的键 它不会包
  • 将事件绑定到 ItemsControl 中的按钮

    我有一个 Windows Phone 7 应用程序 其中包含一些 xaml 如下所示
  • 为什么拆箱枚举会产生奇怪的结果?

    考虑以下 Object box 5 int int int box int 5 int nullableInt box as int nullableInt 5 StringComparison enum StringComparison
  • 如何修复日期过滤器 VBA,因为它没有拾取我范围内的所有日期

    我正在尝试创建一个过滤器来过滤掉我选择的日期内的所有日期 我选择的日期将始终反映整个月 例如 如果我需要 2019 年 5 月的数据 我将输入开始日期为 01 05 2019 结束日期为 31 05 2019 我的数据过滤器将需要选取经过我
  • 如何清除 APC 缓存而不使 Apache 崩溃?

    如果 APC 存储大量条目 清除它们会导致 httpd 崩溃 如果 apc clear cache user 花费的时间超过 phps max execution time 调用 apc clear cache 的脚本 将在之前被 php
  • 单击引导分页链接时调用 jquery 函数

    我想在单击引导分页链接时调用 jquery 函数 假设我想从第1页遍历到第2页 应该调用一个jquery函数 我正在使用以下代码 但它不起作用 ul pagination on click li function alert page ch
  • 在android中跟踪FTP上传数据?

    我有一个运行 Android 的 FTP 系统 但我希望能够在上传时跟踪字节 这样我就可以在上传过程中更新进度条 安卓可以实现这个功能吗 现在 我正在使用org apache common net ftp我正在使用的代码如下 另外 我在 A
  • Android 自定义警报对话框中的 OnClickListener

    我是一个自学成才的初学者 感谢耐心 谢谢 在 Eclipse 中 我使用自己的 xml 文件 custom dialog 创建了一个自定义警报对话框 称为 usernamealert 如果用户尚未输入用户名 即 username lengt