2014-10-09 108 views
0

我是這個「世界」的新手,我對這個按鈕有嚴重的麻煩,我用Android Studio做了更多的按鈕,但是這個讓我瘋狂,我不知道如何解決這個問題,請問你們誰能幫助我?onClick Button不能正常工作

的.java(我只是想創建一個舉杯說:「發送成功」,然後創建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

最有可能是不相關的,但爲什麼一旦單擊按鈕時又設置了監聽器? – Simas 2014-10-09 20:15:23

回答

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

} 
+0

女巫嗎? btAceptar.setOnClickListener(新View.OnClickListener() btAceptar.setOnClickListener(本); 感謝 – 2014-10-09 20:19:51

+0

的btAceptar.setOnClickListener(本); – brwngrldev 2014-10-09 20:21:33

+0

我做到了,但它仍然沒有改變的活動既沒有意圖/束 當我點擊按鈕時,它只會出現吐司...... – 2014-10-09 20:24:01