2010-11-13 92 views
2

我遇到以下代碼的問題,我創建了兩個添加了onClickListener的按鈕。onClickListener中的代碼無法執行

它工作正常的按鈕 - btnAbout,我試圖用另一種方式來處理btnIntro,而不是使用findViewById(...)方法,但是當我單擊btnIntro時語句不會被執行。

爲什麼會發生這種情況?

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

public class TestActivity extends Activity { 
    private final String TAG = "tag"; 
    private Button btnIntro; 
    private Button btnAbout; 
    private View layoutView; 
    private ViewWrapper wrapper; 

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

     btnAbout = (Button) findViewById(R.id.btnAbout); 
     if (btnIntro == null) { 
      LayoutInflater inflator = getLayoutInflater(); 
      layoutView = inflator.inflate(R.layout.main, null, false); 
      wrapper = new ViewWrapper(layoutView); 
      layoutView.setTag(wrapper); 

     } else { 
      wrapper = (ViewWrapper) layoutView.getTag(); 
     } 

     btnIntro = wrapper.getButton(); 
     Log.e(TAG, Integer.toHexString(layoutView.getId()) + ""); 
     Log.e(TAG, btnIntro.getText().toString()); 

     btnIntro.setOnClickListener(new OnClickListener() { 
      { 
       Log.e(TAG, "static"); 
      } 

      @Override 
      public void onClick(View arg0) { 
       Log.e(TAG, "btnIntro clicked"); 
       Toast.makeText(TestActivity.this, "btnIntro", Toast.LENGTH_SHORT) 
        .show(); 
      } 
     }); 

     btnAbout.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View arg0) { 
       Log.e(TAG, "btnAbout clicked"); 
       Toast.makeText(TestActivity.this, "about", Toast.LENGTH_SHORT).show(); 
      } 
     }); 
    } 

    class ViewWrapper { 
     View base; 
     Button btn1; 

     ViewWrapper(View base) { 
      this.base = base; 
     } 

     Button getButton() { 
      if (btn1 == null) { 
       btn1 = (Button) base.findViewById(R.id.btnIntro); 
       Log.e(TAG, btn1.getText().toString()); 
      } 
      return btn1; 
     } 
    } 
} 

main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/MAIN_LAYOUT_XML" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:padding="30dip"> 
    <TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Test" /> 
    <Button 
    android:id="@+id/btnIntro" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="btnIntro" /> 
    <Button 
    android:id="@+id/btnAbout" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="btnAbout" /> 
</LinearLayout> 
+0

請解釋您通過不使用findViewById方法可以獲得什麼。另外,由於layoutView被定義爲一個View類型,所以ViewWrapper類應該擴展View。 – user432209 2010-11-13 13:41:06

回答

0

問題是,使用不同的內容的觀點,以獲得按鈕,一個在創建時調用的setContentView()和第二你通過調用inflator.infate創建() 。因此,您得到的按鈕被放置在不同的內容視圖中(只顯示其中的一個 - 由setContentView創建)。嘗試改爲:

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    LayoutInflater inflator = getLayoutInflater(); 
    layoutView = inflator.inflate(R.layout.main, null, false); 
    setContentView(layoutView); 
    //..anything you need.. 
} 
+0

感謝您的解釋,現在工作正常,我從中學到了很多東西。 :-) – jerry 2010-11-14 04:51:03

+0

不客氣。 – 2010-11-14 09:40:01