How do I add a second button android?

2020-04-27 00:01发布

I´ve got a click listener to an email intent in my app, but I want to add a second button to do another thing? Sorry for the dum question, Im a begginer.

For the peoplewho said, I embedded my code. Thank you.

Heres the entire code:

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", "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")); 

        }}}

2条回答
走好不送
2楼-- · 2020-04-27 00:20

In the onCreate() you can find your buttons by their id in the your layout XML file, and then add onClickListener to it.

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);


}

Once you have initialized your buttons you can then use one onClick() method to handle the button clicks, This can be done for many buttons doing different things by using the if statements where arg0 is the name you assigned to the button.

public void onClick(View arg0) {
if(arg0 == button1){
    //Do Something
}
if(arg0 == button2){
  //Do something
}
}
查看更多
何必那么认真
3楼-- · 2020-04-27 00:27

First define your button variables in your activity/fragment

Button button1;
Button button2;

In the onCreate (activity) or onCreateView (fragment) method, link the button objects to the buttons in your layout, and set their onClickListeners:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

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

        @Override
        public void onClick(View arg0) {
            //do something
        }
    });
    button2 = (Button) findViewById(R.id.button2);
    button2.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            //do something else
        }
    });
}

Your layout file should then have something like this:

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

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button2" />    
查看更多
登录 后发表回答