2013-05-14 151 views
-1

我有按钮“saveStats”似乎不接受点击。按钮不可点击

在模拟器上没有点击动画。

我试图做干净和重新启动Eclipse,没有工作。

我也有CheckBox小部件,工作正常,只有按钮不工作。这里是我的代码:

感谢帮助:)

Button saveStats; 

CheckBox ageCheck, heightCheck, weightCheck, fatCheck; 


protected void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.set_personal_data); 

     saveStats = (Button) findViewById(R.id.saveStats); 

     ageCheck = (CheckBox) findViewById(R.id.checkBoxAge); 
     heightCheck = (CheckBox) findViewById(R.id.checkBoxHeight); 
     weightCheck = (CheckBox) findViewById(R.id.checkBoxWeight);  
     fatCheck = (CheckBox) findViewById(R.id.checkBoxFat); 

     saveStats.setOnClickListener(this); 
     ageCheck.setOnClickListener(this); 
     heightCheck.setOnClickListener(this); 
     weightCheck.setOnClickListener(this); 
     fatCheck.setOnClickListener(this); 

} 

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


    switch(v.getId()){ 

    case R.id.saveStats: 
    { 
     Toast.makeText(this, "working", Toast.LENGTH_LONG).show(); 
      return; 
     } 
    case R.id.checkBoxAge: 
    { 
     if(ageCheck.isChecked()) 
      Toast.makeText(this, "ok", Toast.LENGTH_LONG).show() 
      return; 
    } 
//// 
//// 
//// 
////.... 

按钮XML代码:

<Button 
     android:id="@+id/saveStats" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/textView1" 
     android:text="save" /> 

回答

1

您必须实现OnClickListener()做这种方式

public class ActivityName extends Activity implements OnClickListener 
{ 

然后override您的onClick()

@Override 
public void onClick(View v) 
{ 
    ... 
} 

只是一个说明。另一种方法是在xml中为每个Button声明相同的点击函数,然后在你的java代码中写入该函数。这可以防止您需要implements OnClickListener并在代码中声明Buttons和设置onClickListener()。你做哪种方式取决于你最喜欢什么,但只是另一种选择。这里有一个小例子:

以XML为每个Button声明函数

<Button 
    android:id="@+id/btn1" 
    ... 
    android:onClick="functionName"/> 
<Button 
    android:id="@+id/btn2" 
    ... 
    android:onClick="functionName"/> 

然后在Java代码:

public void functionName(View v) 
{ 
    ... switch on your id just like you would the other way 
} 

的方法只是需要public,接受View作为其只有param,并且与您在您的申报中声明的名称相同xml

+0

我并覆盖其不工作。为什么按钮不工作和CheckBox工作? – tomer 2013-05-14 15:34:11

+0

你需要将你的'return'改为'break'。返回用于退出一个方法并返回'onClick()'中'void'的一些值。 'break'用于退出'switch'。即使这确实起作用,如果您决定在该方法中添加行为,则无论点击了哪个“视图”,该代码都不会运行 – codeMagic 2013-05-14 15:36:38

+0

返回完成该方法,因此交换机也会结束。 我没有得到返回前的代码。 – tomer 2013-05-14 15:39:31

1

您没有使用break语句。确保您的课程实施OnClickListener

@Override 
public void onClick(View v) { 

    switch(v.getId()) 
    { 
    case R.id.saveStats : 
     Toast.makeText(ActivityName.this, "Button clicked", Toast.LENGTH_LONG).show(); 
     break; 
    case R.id.checkBoxAge : 
         //dosomething 
     break; 
    } 

} 

用于显示吐司使用活动上下文。要知道为什么由commonsware在下面的链接

When to call activity context OR application context?

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context=".MainActivity" > 

<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/textView1" 
    android:layout_alignRight="@+id/textView1" 
    android:layout_marginBottom="66dp" 
    android:layout_marginRight="35dp" 
    android:text="Button1" /> 

<Button 
    android:id="@+id/button2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_below="@+id/button1" 
    android:text="Button2" /> 

<CheckBox 
    android:id="@+id/checkBox1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/button2" 
    android:layout_alignParentLeft="true" 
    android:layout_marginBottom="18dp" 
    android:text="CheckBox" /> 
</RelativeLayout> 

检查答案MainActivity.java

public class MainActivity extends Activity implements OnClickListener{ 

CheckBox cb ; 
Button b,b1; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    b= (Button) findViewById(R.id.button1); 
    b1= (Button) findViewById(R.id.button2); 
    cb = (CheckBox) findViewById(R.id.checkBox1); 
    cb.setOnClickListener(this); 
    b.setOnClickListener(this); 
    b1.setOnClickListener(this); 

} 

@Override 
public void onClick(View v) { 
    // TODO Auto-generated method stub 
    switch(v.getId()) 
    { 
    case R.id.button1 : 
      Toast.makeText(MainActivity.this, "Button clicked1", Toast.LENGTH_LONG).show(); 
      break; 
    case R.id.button2 : 
      Toast.makeText(MainActivity.this, "Button clicked2", Toast.LENGTH_LONG).show(); 
      break; 
    case R.id.checkBox1: 
     if(cb.isChecked()) 
      Toast.makeText(MainActivity.this, "check box clicked", Toast.LENGTH_LONG).show(); 
      break; 

    }  
} 
} 
+0

我不需要休息,我施展回报。 – tomer 2013-05-14 15:34:37

+0

由于onClick方法的返回类型为void – Raghunandan 2013-05-14 15:35:44

+1

@tomer使用break语句并且还使用活动上下文来代替显示toast – Raghunandan 2013-05-14 15:38:23