2013-05-14 160 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