我是这个"世界"我用这个按钮遇到了严重的麻烦,我已经用Android Studio做了更多按钮,但这个让我发疯,我不知道如何解决这个问题,你们中的任何人都可以请帮帮我?
.java(我只是想创建一个toast说"成功发送",然后创建onClickButton来完成剩下的工作)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
final Button btAceptar = (Button) findViewById(R.id.btEnviar);
btAceptar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
btAceptar.setOnClickListener(this);
Toast.makeText(getBaseContext(),"Datos enviados correctamente", Toast.LENGTH_SHORT).show();
}
});
}
public void onClickEnviar(View v){
final EditText txtCognom = (EditText) findViewById(R.id.etCognom);
final EditText txtNom = (EditText) findViewById(R.id.etNom);
final EditText txtEdat = (EditText) findViewById(R.id.etEdat);
final EditText txtTelefon = (EditText) findViewById(R.id.etTelefon);
final EditText txtEmail = (EditText) findViewById(R.id.etEmail);
Intent iIntent = new Intent(MyActivity.this, EnvioFormulario.class);
// Bundle per al cognom i posem la informació al intent
Bundle bEnviat = new Bundle();
bEnviat.putString("Cognom", txtCognom.getText().toString());
bEnviat.putString("Nom", txtNom.getText().toString());
bEnviat.putString("Edat", txtEdat.getText().toString());
bEnviat.putString("Telefon", txtTelefon.getText().toString());
bEnviat.putString("Email", txtEmail.getText().toString());
iIntent.putExtras(bEnviat);
// Start a la activitat per a que començi
startActivity(iIntent);
}
如果您需要我的代码的其他部分来试图帮助我,我会发布它!
感谢。
答案 0 :(得分:1)
从侦听器内部删除onClickListener的设置。这不是必需的。请保留此部分:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
final Button btAceptar = (Button) findViewById(R.id.btEnviar);
btAceptar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onClickEnviar();
Toast.makeText(getBaseContext(),"Datos enviados correctamente", Toast.LENGTH_SHORT).show();
});
}
public void onClickEnviar() {
final EditText txtCognom = (EditText) findViewById(R.id.etCognom);
final EditText txtNom = (EditText) findViewById(R.id.etNom);
final EditText txtEdat = (EditText) findViewById(R.id.etEdat);
final EditText txtTelefon = (EditText) findViewById(R.id.etTelefon);
final EditText txtEmail = (EditText) findViewById(R.id.etEmail);
Intent iIntent = new Intent(MyActivity.this, EnvioFormulario.class);
// Bundle per al cognom i posem la informació al intent
Bundle bEnviat = new Bundle();
bEnviat.putString("Cognom", txtCognom.getText().toString());
bEnviat.putString("Nom", txtNom.getText().toString());
bEnviat.putString("Edat", txtEdat.getText().toString());
bEnviat.putString("Telefon", txtTelefon.getText().toString());
bEnviat.putString("Email", txtEmail.getText().toString());
iIntent.putExtras(bEnviat);
// Start a la activitat per a que començi
startActivity(iIntent);
}