如何在android中添加第二个按钮?
我的应用程序中有一个电子邮件目的的点击侦听器,但我想添加第二个按钮来做另一件事?对不起,我是个乞丐 对于那些说我嵌入了我的代码的人。多谢各位 以下是全部代码:如何在android中添加第二个按钮?,android,eclipse,button,onclicklistener,Android,Eclipse,Button,Onclicklistener,我的应用程序中有一个电子邮件目的的点击侦听器,但我想添加第二个按钮来做另一件事?对不起,我是个乞丐 对于那些说我嵌入了我的代码的人。多谢各位 以下是全部代码: 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.Doc
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"));
}}}
首先在活动/片段中定义按钮变量
Button button1;
Button button2;
在onCreate活动或onCreateView片段方法中,将按钮对象链接到布局中的按钮,并设置其OnClickListener:
然后,布局文件应具有如下内容:
<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" />
在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
}
}
这与其说是一个愚蠢的问题,不如说是一个不完整的问题。无论如何,请看“是”中的“是”中的“是”中的“是”,请向我们展示您迄今为止所做的尝试。idea将提供一个编译和运行的简化代码示例。看,我转载了代码。你看到我链接到的答案了吗?这是一种将多个按钮与单个功能结合使用的好方法,它表示不能打开布尔类型的值。只允许使用可转换的int值或enum变量。它说在类型Main Activity中使用复制方法OnClickView。我添加了代码。我的第二个按钮是创建一个PDF,其中包含我已经测试和使用过的android文本。听起来你好像要设置onClickListener两次,可能吧?确保变量名正确无误,即button1.setonclicklistener&button2.setonclicklistener也看一看这个。如果要在XML中设置onClick=clickFunction功能,那么只需要在代码中定义函数,而不是连接onClickListeners
public void onClick(View arg0) {
if(arg0 == button1){
//Do Something
}
if(arg0 == button2){
//Do something
}
}