2010-09-03 70 views
2

我的Activity下面的onClick()似乎没有做任何事情(没有看到任何字符串出现),但我没有得到任何错误。我错过了什么?有没有办法追踪功能?android:button onClick(),不能告诉是否有任何事情发生

package com.HelloTabWidget2; 

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

public class AlbumsActivity extends Activity { 

    private Button closeButton; 

public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     this.setContentView(R.layout.tab1); 
     this.closeButton = (Button)this.findViewById(R.id.button); 
     this.closeButton.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
      Toast.makeText(AlbumsActivity.this, "You clicked the button", Toast.LENGTH_SHORT).show(); 

      } 
     }); 


    } 
} 

谢谢!

回答

3

我相信问题是使用匿名方法,当尝试使用您的代码时出现错误。只需添加暗示OnClickListenter。

如果您有多个按钮,您需要在v.getId()上添加一个开关或其他东西。

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 AlbumsActivity extends Activity implements OnClickListener { 
    private Button closeButton; 

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

     this.closeButton = (Button)this.findViewById(R.id.button); 
     this.closeButton.setOnClickListener(this); 
    } 

    @Override 
    public void onClick(View v) { 
     Toast.makeText(this, "You clicked the button", Toast.LENGTH_SHORT).show(); 

    } 
} 
+0

这应该工作。此外,性能方面的原因更好。在问题中注册侦听器会在运行时创建新对象。 – Juri 2010-09-03 15:29:07

+0

是的,但活动不是使用onClickListener的,它是按钮。如果您尝试通过这样做来节省性能,则不会使用面向对象的设计概念。除非您有特定的理由,否则请不要这样做。 – Falmarri 2010-09-03 20:24:08

1

我要说的是,而不是写在下面的代码

...setOnClickListener(new OnClickListener() {..." 

我宁愿以下OCDE

"...setOnClickListener(new View.OnClickListener() {..." 
+0

事实上,这应该可以解决这个问题。 – Juri 2010-09-03 15:28:15

+0

我已经尝试了上面列出的两种方法,但我仍然没有收到任何指示,无论是从我的祝词还是日志。还有什么我失踪? – JoshuaBen 2010-09-07 14:41:44

+0

好吧,如果我将其设置为新项目中的独立活动,它可以正常工作。然而,当我把它作为一个标签内的活动时,我什么都看不到。这是为什么它打破了? – JoshuaBen 2010-09-07 15:01:35

0

您可以简单地使用下面的代码

Button closeButton = (Button) findViewById(R.id.button); 

    closeButton.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      Toast.makeText(getApplicationContext(), "Onclick works", Toast.LENGTH_LONG).show(); 

     } 
    }); 

如果它提供例外,那就去吧做项目 - >清洁。事情会正常工作

-1
public class Whatever_MainActivity extends Activity 
{ 
    ImageButton button1, button2, button3, button4, button5;  // my buttons 
    Button start, reroll, hold; 

    Button reroll_dice1, reroll_dice2, reroll_dice3, reroll_dice4, reroll_dice5; 

    Button ok_button1; 

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

     Buttons(); // "Come here button" 

     // Scoring_Method(); 
    } 

public void Buttons() 
    { 
     ok_button1 = (Button)findViewById(R.id.ok_button1); 
     ok_button1.setOnClickListener(new OnClickListener() 
         { 
          public void onClick(View buttonn) 
          { 
           reroll_dice1.setVisibility(View.INVISIBLE); 
           reroll_dice2.setVisibility(View.INVISIBLE); 
           reroll_dice3.setVisibility(View.INVISIBLE); 
           reroll_dice4.setVisibility(View.INVISIBLE); 
           reroll_dice5.setVisibility(View.INVISIBLE); 
           ok_button1.setVisibility(View.INVISIBLE); 


           reroll.setVisibility(View.VISIBLE); 
           hold.setVisibility(View.VISIBLE); 
          } 
         }); 
    } 
+0

你可能会考虑编辑这篇文章并解释你的代码如何解决OP问题。 OP还对跟踪错误提出了一些问题。 – 2012-06-11 05:43:45

相关问题