如何添加第二个按钮android?

如何添加第二个按钮android?,第1张

概述我的应用程序中有一个指向电子邮件意图的单击侦听器,但是我想添加第二个按钮来执行其他 *** 作?对不起,我是个初学者.对于那些说的人,我嵌入了我的代码.谢谢.这是完整的代码:packagecom.example.kapscht;importjava.io.File;importjava.io.FileOutputStream;importjava.io.I

我的应用程序中有一个指向电子邮件意图的单击侦听器,但是我想添加第二个按钮来执行其他 *** 作?对不起,我是个初学者.

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

这是完整的代码:

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;@OverrIDeprotected 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); }@OverrIDepublic 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", "villasantdesign@gmail.com", null));        intentEmail.putExtra(androID.content.Intent.EXTRA_EMAIL, new String[]{"villasantdesign@gmail.com"});        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}}
总结

以上是内存溢出为你收集整理的如何添加第二个按钮android?全部内容,希望文章能够帮你解决如何添加第二个按钮android?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1093249.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存