2013-04-27 103 views
-1
  package com.gofitness.www; 

      import android.app.Activity; 


      import android.os.Bundle 
      import android.view.View; 
      import android.view.View.OnClickListener; 
      import android.widget.Button; 
      import android.widget.EditText; 
      import android.widget.TextView; 


      public class log extends Activity implements OnClickListener { 

    Button submit,bEdit, bGetInfo, bDelete; 
    TextView display; 
    EditText etRowInfo, username, password; 




    protected void onCreate(Bundle login) { 
      // TODO Auto-generated method stub 
      super.onCreate(login); 
    setContentView(R.layout.insert); 

    submit = (Button) findViewById(R.id.submit); 
    username = (EditText) findViewById(R.id.etUsername); 
    password = (EditText) findViewById(R.id.etPassword); 
    display = (TextView) findViewById(R.id.tvDisplay); 
bEdit = (Button) findViewById(R.id.bEdit); 
etRowInfo = (EditText) findViewById(R.id.etRowInfo); 
bGetInfo = (Button) findViewById(R.id.bGetInfo); 
bDelete = (Button) findViewById(R.id.bDelete); 

    submit.setOnClickListener(this); 
    etRowInfo.setOnClickListener(this); 
    } 
      public void onClick(View arg0) { 
        switch (arg0.getId()){ 
        case R.id.submit: 
          display.setText("fdf"); 
        boolean didItWork = true; 

        try{ 
        String check = username.getText().toString(); 
        String checkpass = password.getText().toString(); 

        logindetails entry = new logindetails(log.this); 
        entry.open(); 
        entry.createEntry(check, checkpass); 
        entry.close(); 


        }catch (Exception e){ 
        didItWork = false; 
        String error = e.toString(); 
        display.setText(error); 
        }finally{ 
        if (didItWork){ 

        display.setText("WERKED"); 
        } 
        } 





        break; 
        case R.id.bGetInfo: 
          display.setText("fdf"); 
          String s = etRowInfo.getText().toString(); 
          long l = Long.parseLong(s); 
          logindetails log = new logindetails(this); 
          log.open(); 
          String returnedName = log.getName(l); 
          String returnedPassword = log.getPassword(l); 
          log.close(); 

          password.setText(returnedPassword); 

          break; 


        case R.id.bEdit: 
          display.setText("fdf"); 


          break; 



        case R.id.bDelete: 
          display.setText("fdf"); 


          break; 



      } 





    } 
    } 

看起來第一個案件的作品和按鈕改變了顯示文本視圖,但其餘的按鈕不起作用。一旦點擊沒有發生,但沒有錯誤。Android開發開關案例問題

任何人都知道爲什麼會發生這種情況?

乾杯

回答

0

您沒有在所有按鈕上設置onClickListeners。

只有這兩個:

submit.setOnClickListener(this); 
etRowInfo.setOnClickListener(this); 

添加這些:

bEdit.setOnClickListener(this); 
bDelete.setOnClickListener(this); 
bGetInfo.setOnClickListener(this); 
0

你缺少你的onClick方法之前寫@override,因爲它是接口,因此我們需要把它寫或採取幫助從下面的代碼

import android.app.Activity; 
    import android.os.Bundle; 
    import android.view.View; 
    import android.view.View.OnClickListener; 
    import android.widget.Button; 
    import android.widget.Toast; 

     public class MainActivity extends Activity implements OnClickListener { 
     Button b1,b2; 
      @Override 
      protected void onCreate(Bundle savedInstanceState) { 
       super.onCreate(savedInstanceState); 
       setContentView(R.layout.activity_main); 
       b1=(Button)findViewById(R.id.button1); 

       b2=(Button)findViewById(R.id.button2); 
       b1.setOnClickListener(this); 
       b2.setOnClickListener(this); 
      } 



      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 

       int id=v.getId(); 
       switch(id) { 
        case R.id.button1: 
        Toast.makeText(getBaseContext(), "btn1", Toast.LENGTH_LONG).show(); 
       //Your Operation 

        break; 

        case R.id.button2: 
          Toast.makeText(getBaseContext(), "btn2", Toast.LENGTH_LONG).show(); 


          //Your Operation 
          break; 
      } 

     }}