如何添加第二个按钮 android?

2024-01-31

我的应用程序中有一个电子邮件意图的点击侦听器,但我想添加第二个按钮来执行另一件事?抱歉问这个愚蠢的问题,我是初学者。

对于那些说的人来说,我嵌入了我的代码。谢谢。

完整代码如下:

package com.example.kapscht;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import com.example.kapscht.R;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;

import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.graphics.Color;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.util.Date;
import java.text.DateFormat;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.view.View.OnClickListener;

public class MainActivity extends Activity implements OnClickListener{
CheckBox checkgrua1; 
CheckBox checkreader1; 
CheckBox checkreader2;
CheckBox checkctm1;
CheckBox checkctm2; 
CheckBox checkpsm1; 
CheckBox checkpsm2;
CheckBox checklbm1;
CheckBox checklbm2;
CheckBox checkrx1;
CheckBox checkrx2; 
CheckBox checktx1; 
CheckBox checktx2;
CheckBox checkmodulerf1;
CheckBox checkmodulerf2; 
CheckBox checkmodulemrf1;
CheckBox checkmodulemrf2;
CheckBox checkcablerf1; 
CheckBox checkcablerf2;
CheckBox checkconectorrf1;
CheckBox checkconectorrf2;
CheckBox checkatenuacionrf1;
CheckBox checkatenuacionrf2; 
CheckBox checkloopback1; 
CheckBox checkloopback2;
CheckBox checkfirmware1;
CheckBox checkfirmware2; 
CheckBox checkethernet1; 
CheckBox checkethernet2;
CheckBox checkamplificador1;
CheckBox checkamplificador2;
CheckBox checkfp1;
CheckBox checkfp2; 
CheckBox checksincro1; 
CheckBox checksincro2;
CheckBox checkotro1;
CheckBox checkotro2;
EditText etcliente;
EditText etsitio; 
EditText etrampa;
EditText etfalla;
EditText etdescripcion;
EditText etqg;
EditText etprevio;
EditText etdejarsitio;
TextView vwfecha1; 
TextView vwfecha2; 
Button pdf;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Date date = new Date();
    String dateTime = DateFormat.getDateTimeInstance().format(date);
    vwfecha1 = (TextView) findViewById(R.id.vwfecha1);
    vwfecha1.setText(dateTime);
    checkgrua1= (CheckBox) findViewById(R.id.checkgrua1);
    checkreader1= (CheckBox) findViewById(R.id.checkreader1);
    checkreader2= (CheckBox) findViewById(R.id.checkreader2);
    checkctm1= (CheckBox) findViewById(R.id.checkctm1);
    checkctm2= (CheckBox) findViewById(R.id.checkctm2);
    checkpsm1= (CheckBox) findViewById(R.id.checkpsm1);
    checkpsm2= (CheckBox) findViewById(R.id.checkpsm2);
    checklbm1= (CheckBox) findViewById(R.id.checklbm1);
    checklbm2= (CheckBox) findViewById(R.id.checklbm2);
    checkrx1= (CheckBox) findViewById(R.id.checkrx1);
    checkrx2= (CheckBox) findViewById(R.id.checkrx2);
    checktx1= (CheckBox) findViewById(R.id.checktx1);
    checktx2= (CheckBox) findViewById(R.id.checktx2);
    checkmodulerf1= (CheckBox) findViewById(R.id.checkmodulerf1);
    checkmodulerf2= (CheckBox) findViewById(R.id.checkmodulerf2);
    checkmodulemrf1= (CheckBox) findViewById(R.id.checkmodulemrf1);
    checkmodulemrf2= (CheckBox) findViewById(R.id.checkmodulemrf2);
    checkcablerf1= (CheckBox) findViewById(R.id.checkcablerf1);
    checkcablerf2= (CheckBox) findViewById(R.id.checkcablerf2);
    checkconectorrf1= (CheckBox) findViewById(R.id.checkconectorrf1);
    checkconectorrf2= (CheckBox) findViewById(R.id.checkconectorrf2);
    checkatenuacionrf1= (CheckBox) findViewById(R.id.checkatenuacionrf1);
    checkatenuacionrf2= (CheckBox) findViewById(R.id.checkatenuacionrf2);
    checkloopback1= (CheckBox) findViewById(R.id.checkloopback1);
    checkloopback2= (CheckBox) findViewById(R.id.checkloopback2);
    checkfirmware1= (CheckBox) findViewById(R.id.checkfirmware1);
    checkfirmware2= (CheckBox) findViewById(R.id.checkfirmware2);
    checkethernet1= (CheckBox) findViewById(R.id.checkethernet1);
    checkethernet2= (CheckBox) findViewById(R.id.checkethernet2);
    checkamplificador1= (CheckBox) findViewById(R.id.checkamplificador1);
    checkamplificador2= (CheckBox) findViewById(R.id.checkamplificador2);
    checkfp1= (CheckBox) findViewById(R.id.checkfp1);
    checkfp2= (CheckBox) findViewById(R.id.checkfp2);
    checksincro1= (CheckBox) findViewById(R.id.checksincro1);
    checksincro2= (CheckBox) findViewById(R.id.checksincro2);
    checkotro1= (CheckBox) findViewById(R.id.checkotro1);
    checkotro2= (CheckBox) findViewById(R.id.checkotro2);
    etcliente= (EditText) findViewById(R.id.etcliente);
    etrampa= (EditText) findViewById(R.id.etrampa);
    etprevio= (EditText) findViewById(R.id.etprevio);
    etdejarsitio= (EditText) findViewById(R.id.etdejarsitio);
    etsitio= (EditText) findViewById(R.id.etsitio);
    etfalla= (EditText) findViewById(R.id.etfalla); 
    etdescripcion= (EditText) findViewById(R.id.etdescripcion);
    etqg= (EditText) findViewById(R.id.etqg);
    View boton = (Button) findViewById(R.id.enviar);
    boton.setOnClickListener(this); 


}

@Override
public void onClick(View v) {
    if (v.getId()==findViewById(R.id.enviar).getId())
    {
        Date date = new Date();
        String dateTime2 = DateFormat.getDateTimeInstance().format(date);
        vwfecha2 = (TextView) findViewById(R.id.vwfecha1);
        vwfecha2.setText(dateTime2);
        Intent intentEmail = new Intent(Intent.ACTION_SEND, Uri.fromParts("mailto", "[email protected] /cdn-cgi/l/email-protection", null));
        intentEmail.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"[email protected] /cdn-cgi/l/email-protection"});
        intentEmail.putExtra(Intent.EXTRA_SUBJECT, "Consulta Técnica");
        intentEmail.putExtra(Intent.EXTRA_TEXT, "Fecha/hora abierto: "
        +vwfecha1.getText().toString()
        +"\n"
        +"\nFecha/hora cerrado: "
        +vwfecha2.getText().toString()
        +"\n"
        +"\nCliente:  "+etcliente.getText()
        +"\n"
        +"\nSitio:  "+etsitio.getText()
        +"\n"
        +"\nRampa:  "+etrampa.getText()
        +"\n"
        +"\nSe usó grua:  "+checkgrua1.isChecked()
        +"\n"
        +"\nEstado del equipo  "
        +"\nPrevio a la visita:  "+etprevio.getText()
        +"\nAl dejar el sitio:  "+etdejarsitio.getText()
        +"\n"
        +"\nFalla Reportada:  "+etfalla.getText()
        +"\n"
        +"\nSe reemplazó reader/lector: "+checkreader1.isChecked()
        +"\nSe ajustó reader/lector: "+checkreader2.isChecked()
        +"\n"
        +"\nSe reemplazó CTM:  "+checkctm1.isChecked()
        +"\nSe ajustó CTM:  "+checkctm2.isChecked()
        +"\n"
        +"\nSe reemplazó PSM: "+checkpsm1.isChecked()
        +"\nSe ajustó PSM: "+checkpsm2.isChecked()
        +"\n"
        +"\nSe reemplazó LBM:  "+checklbm1.isChecked()
        +"\nSe ajustó LBM:  "+checklbm2.isChecked()
        +"\n"
        +"\nSe reemplazó Antena Rx: "+checkrx1.isChecked()
        +"\nSe ajustó Antena Rx: "+checkrx2.isChecked()
        +"\n"
        +"\nSe reemplazó Antena Tx:  "+checktx1.isChecked()
        +"\nSe ajustó Antena Tx:  "+checktx2.isChecked()
        +"\n"
        +"\nSe reemplazó Módulo RF: "+checkmodulerf1.isChecked()
        +"\nSe ajustó Módulo RF: "+checkmodulerf2.isChecked()
        +"\n"
        +"\nSe reemplazó Módulo MRF:  "+checkmodulemrf1.isChecked()
        +"\nSe ajustó Módulo MRF:  "+checkmodulemrf2.isChecked()
        +"\n"
        +"\nSe reemplazó Cable/Jumper RF: "+checkcablerf1.isChecked()
        +"\nSe ajustó Cable/Jumper RF: "+checkcablerf2.isChecked()
        +"\n"
        +"\nSe reemplazó Conector RF:  "+checkconectorrf1.isChecked()
        +"\nSe ajustó Conector RF:  "+checkconectorrf2.isChecked()
        +"\n"
        +"\nSe reemplazó Atenuación RF: "+checkatenuacionrf1.isChecked()
        +"\nSe ajustó Atenuación RF: "+checkatenuacionrf2.isChecked()
        +"\n"
        +"\nSe reemplazó Cable Loopback RF: "+checkloopback1.isChecked()
        +"\nSe ajustó Cable Loopback RF: "+checkloopback2.isChecked()
        +"\n"
        +"\nSe reemplazó SW/Firmware: "+checkfirmware1.isChecked()
        +"\nSe ajustó SW/Firmware: "+checkfirmware2.isChecked()
        +"\n"
        +"\nSe reemplazó Conexión Ethernet:  "+checkethernet1.isChecked()
        +"\nSe ajustó Conexión Ethernet:  "+checkethernet2.isChecked()
        +"\n"
        +"\nSe reemplazó Amplificador: "+checkamplificador1.isChecked()
        +"\nSe ajustó Amplificador: "+checkamplificador2.isChecked()
        +"\n"
        +"\nSe reemplazó FP Amp.:  "+checkfp1.isChecked()
        +"\nSe ajustó FP Amp.:  "+checkfp2.isChecked()
        +"\n"
        +"\nSe reemplazó Sincronización: "+checksincro1.isChecked()
        +"\nSe ajustó Sincronización: "+checksincro2.isChecked()
        +"\n"
        +"\nSe reemplazó OTRO:  "+checkotro1.isChecked()
        +"\nSe ajustó OTRO:  "+checkotro2.isChecked()
        +"\n"
        +"\nDescripción de reparación: "+etdescripcion.getText()
        +"\n"
        +"\nQuien genera: "+etqg.getText());            
        intentEmail.setType("message/rfc822");
        startActivity(Intent.createChooser(intentEmail, "Enviar")); 

        }}}

在 onCreate() 中,您可以通过布局 XML 文件中的 id 找到按钮,然后向其中添加 onClickListener。

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.yourLayout); 

    button1 = (Button)findViewById(R.id.b1);
    button1.setOnClickListener(this);

    button2 = (Button)findViewById(R.id.b2);
    button2.setOnClickListener(this);


}

一旦初始化了按钮,您就可以使用一个 onClick() 方法来处理按钮单击,这可以通过使用 if 语句(其中 arg0 是您分配给按钮的名称)来执行不同操作的许多按钮来完成。

public void onClick(View arg0) {
if(arg0 == button1){
    //Do Something
}
if(arg0 == button2){
  //Do something
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何添加第二个按钮 android? 的相关文章

  • 从 Throwable 获取错误代码 - Android

    我怎样才能从错误代码可投掷 https developer android com reference java lang Throwable html public void onFailure Throwable exception 我
  • Android SoundPool 堆限制

    我正在使用 SoundPool 加载多个声音剪辑并播放它们 据我所知 它的功能 100 正确 但在 load 调用期间 我的日志中充斥着以下内容 06 09 11 30 26 110 ERROR AudioCache 23363 Heap
  • Firebase Analytics 禁用受众国家/地区跟踪

    我正在开发一个严格不允许位置跟踪的应用程序 我想使用 Firebase Analytic 的其他功能 例如 PageTransitions 和 Crashalitics 但如果我无法禁用受众位置跟踪 我就无法使用其中任何功能 这是我在 An
  • 如果我们使用后退按钮退出,为什么 Android 应用程序会重新启动?

    按住主页按钮并返回应用程序时 应用程序不会重新启动 为什么使用后退按钮会重新启动 如果我们使用后退按钮退出 有什么方法可以解决在不重新启动的情况下获取应用程序的问题吗 请帮忙 当您按下Home按钮 应用程序将暂停并保存当前状态 最后应用程序
  • 在 Android 中使用 DataOutputStream 在 POST 正文中发送特殊字符 (ë ä ï)

    我目前正在开发一个具有大量服务器端通信的 Android 应用程序 昨天 我收到一份错误报告 称用户无法发送 简单 特殊字符 例如 我搜索过但没有找到任何有用的东西 可能重复 没有答案 https stackoverflow com que
  • 在意图过滤器中使用多个操作时的默认值

    尝试理解 Android 中的意图和操作并查看文档 http developer android com guide topics intents intents filters html 但我一直看到的一件事是定义了多个操作的意图过滤器
  • OnClick 事件中的 finish() 如何工作?

    我有一个Activity一键退出Activity 通过layout xml我必须设置OnClick事件至cmd exit调用 this finish 效果很好 public void cmd exit View editLayout thi
  • Firebase:如何在Android应用程序中设置默认通知渠道?

    如何设置default通知渠道通知消息当应用程序在后台运行时会出现什么情况 默认情况下 这些消息使用 杂项 通道 如你看到的在官方文档中 https firebase google com docs cloud messaging andr
  • 请求位置更新参数

    这就是 requestLocationUpdates 的样子 我使用它的方式 requestLocationUpdates String provider long minTime float minDistance LocationLis
  • 带有自定义阵列适配器的微调器不允许选择项目

    我使用自定义阵列适配器作为微调器 但是 当在下拉列表中选择一个项目时 下拉列表保留在那里 并且微调器不会更新 这是错误行为 与使用带有字符串的通用数组适配器相比 这是自定义类 我错过了什么吗 谢谢 public class Calendar
  • 检查 Android 手机上的方向

    如何查看Android手机是横屏还是竖屏 当前配置用于确定要检索的资源 可从资源中获取Configuration object getResources getConfiguration orientation 您可以通过查看其值来检查方向
  • 从 android 简单上传到 S3

    我在网上搜索了从 android 上传简单文件到 s3 的方法 但找不到任何有效的方法 我认为这是因为缺乏具体步骤 1 https mobile awsblog com post Tx1V588RKX5XPQB TransferManage
  • Android相机意图:如何获取全尺寸照片?

    我正在使用意图来启动相机 Intent cameraIntent new Intent android provider MediaStore ACTION IMAGE CAPTURE getParent startActivityForR
  • Android Studio:无法启动守护进程

    当我尝试在 Android Studio 中导入 gradle 项目时 遇到以下错误 Unable to start the daemon process This problem might be caused by incorrect
  • 材质设计图标颜色

    应该是哪种颜色 暗 材质图标 在官方文档上 https www google com design spec style icons html icons system icons https www google com design s
  • 如何创建像谷歌位置历史记录一样的Android时间轴视图?

    我想设计像谷歌位置历史这样的用户界面 我必须为我正在使用的应用程序复制此 UIRecyclerView 每行都是水平的LinearLayout其中包含右侧的图标 线条和视图 该线是一个FrameLayout具有圆形背景和半透明圆圈Views
  • Android 设备上的静默安装

    我已经接受了一段时间了 在 Android 上静默安装应用程序是不可能的 也就是说 让程序安装捆绑为 APK 的应用程序 而不提供标准操作系统安装提示并完成应用程序安装程序活动 但现在我已经拿到了 Appbrain 快速网络安装程序的副本
  • 如何将设备连接到Eclipse?

    我无法解决这个简单的问题 我正在尝试通过 USB 电缆将我的设备连接到 Eclipse 在我的 PC 上 我已经安装了 Eclipse 和 Android SDK 并且在模拟器上运行该程序运行良好 我已在我的电脑上下载并安装了 Samsun
  • 将 JSON 参数从 java 发布到 sinatra 服务

    我有一个 Android 应用程序发布到我的 sinatra 服务 早些时候 我无法读取 sinatra 服务上的参数 但是 在我将内容类型设置为 x www form urlencoded 之后 我能够看到参数 但不完全是我想要的 我在
  • Android - 将 ImageView 保存到具有全分辨率图像的文件

    我将图像放入 ImageView 中 并实现了多点触控来调整 ImageView 中的图像大小和移动图像 现在我需要将调整大小的图像保存到图像文件中 我已经尝试过 getDrawingCache 但该图像具有 ImageView 的大小 我

随机推荐

  • 您可以从 UUID 中提取什么类型的数据?

    我知道我们可以轻松提取 uuid 版本号 有没有可靠的方法来提取时间戳 MAC 地址等信息 Thanks 符合标准的 UUID 可能是几种变体之一 它看起来像这样 AAAAAAAA BBBB CCCC DDDD FFFFFFFFFFFF D
  • 从函子模板参数推导出可变参数和返回类型(MSVC 特定)

    功能invoke下面的代码是一个简单的包装器 用于调用另一个函数 仿函数 lambda 这样invoke f args equals f args 其背后的原因是还具有成员函数的重载 从而允许两种用法的通用语法 这个实现在 g 中工作 te
  • 如何使用 C# 从 XML 文件中删除节点[重复]

    这个问题在这里已经有答案了 可能的重复 如何从 XmlNodeList 中删除 XmlNode https stackoverflow com questions 875136 how to remove an xmlnode from x
  • 测试房间迁移,在资产文件夹[room-migration]中找不到架构文件

    这个问题是在我决定向房间数据库添加另一个实体之后出现的 架构正在预期目录中导出 所有 build gradle 设置均已完成 并且似乎有效 但实际上无效 自从我得到 java io FileNotFoundException Cannot
  • Mockito 等价于expect().andReturn().times() 的是什么

    我一直在尝试 Mockito 的等效项 EasyMock expect someMethod andReturn someMockObject times n 但我不明白 请对此提供一点帮助吗 提前致谢 when myObject some
  • 将远程 (Paramiko) ssh 命令的输出评估为成功/失败布尔值

    我有一个函数检查文件是否存在 它返回 True False 现在我正在将其 转换 为布尔值eval 但是我不认为这是最明智的解决方案 但我不确定在没有不必要的情况下还能如何做到这一点ifs gt gt gt foo False gt gt
  • 带有正则表达式的标签-普罗米修斯

    我正在尝试使用正则表达式添加新标签 名称实例是pr na01 na02 A我试图只得到pr na01 所以我这样做了 source labels meta ec2 tag Name regex target label test repla
  • 如何以编程方式获取上次迁移的名称?

    我想获取 Django 中最后应用的迁移的名称 我知道 Django 迁移存储在django migrations然而表django db migrations migration Migration不是一个models Model在那张桌
  • 是否有 Jackson 注释来抑制不必要的 JSON 包装?

    我正在序列化的类 public class LogsDTO The logs private List
  • 我需要为所有 Android 应用程序使用相同的证书吗?

    如果不同的应用程序都在同一个 Google Play 帐户下发布 我是否需要使用不同的证书来签署它们 或者我可以始终使用相同的证书吗 如果没有特殊要求 最佳实践是什么 您无需使用同一证书在同一开发者帐户上发布所有应用程序 并且每个应用程序可
  • 如何将 blob 转换为 base64?

    axios get RequestURL responseType blob withCredentials false then response gt let imageNode document getElementById imag
  • 音位间隙和提示()

    我正在浏览Phonegap http www phonegap com Android 的来源 并试图验证他们的notification alert 方法只是委托给原生 JavaScriptalert 功能 他们的代码做了 Notifica
  • JWT 的 ES6 导入

    我正在制作一个 NodeJS Web 应用程序 并使用 JWT 进行身份验证 我的所有工作都在 ES6 模块中 我想以相同的方式导入 JWT 但显然该包尚不支持它 我无法使用旧的 require 格式 因为它会抛出错误 因为我已将其设置为
  • 为什么config文件夹没有被推送?

    尽管我确信在推送之前我已将所有文件和文件夹添加并提交到我的本地 git 存储库中 我不知道为什么远程仓库缺少配置文件夹 我不知道如何调查这个问题 当然我没有将配置文件夹名称添加到 gitignore 或 slugignore 中 但是 它永
  • 匹配浮点范围的替代方案

    编写一系列浮点范围比较的最佳方法是什么 要使用下面 GitHub 评论中的示例 let color match foo 0 0 0 1 gt Color Red 0 1 0 4 gt Color Yellow 0 4 0 8 gt Colo
  • 有什么方法可以在 Slick 中进行多次插入/更新吗?

    在sql中我们可以这样做 INSERT INTO tbl name a b c VALUES 1 2 3 4 5 6 7 8 9 有什么方法可以在 Slick 中进行多次 批量 批量插入或更新吗 我们可以做类似的事情吗 至少使用SQL 普通
  • FCM 使用 firebase-admin 库通过防火墙发送消息

    在公司我们有防火墙 我们已经启用了fcm googleapis com433地址 但这并没有帮助 jboss 应用程序仍然无法使用发送推送请求com google firebase firebase admin图书馆 我知道 IP 范围 但
  • Android bitmap.h 未找到用于本机代码编译

    我正在尝试使用 Android NDK 编译代码 我以前很好地使用过这段代码 但改变了其中的一件事 现在它无法编译 显示的错误是 Compile thumb filters lt filters cpp C Android my app j
  • 从数据表中,每组随机选择一行

    我正在寻找一种从数据表中选择行的有效方法 以便为特定列中的每个唯一值提供一个代表行 让我提出一个简单的例子 require data table y c a b c d e f g h x sample 2 10 8 replace TRU
  • 如何添加第二个按钮 android?

    我的应用程序中有一个电子邮件意图的点击侦听器 但我想添加第二个按钮来执行另一件事 抱歉问这个愚蠢的问题 我是初学者 对于那些说的人来说 我嵌入了我的代码 谢谢 完整代码如下 package com example kapscht impor