2011-06-16 61 views
0

我有一個帶有4個選項卡的TabActivity。當點擊其中一個選項卡中的按鈕並啓動一個新的活動(新的活動不在TabHost內)時,新的活動不會註冊OnClick()。新的活動甚至不能顯示吐司,這讓我覺得TabHost以某種方式阻止了用戶界面?Android:從TabHost開始一項新活動會禁用onClick

當把Activity作爲標籤之一的時候,OnClick工作得很好。 任何想法這是什麼原因?

我包含3類:

1)TabActivity

2)開始於一個選項卡中的活動:

3)不能註冊的OnClick新活動()


1)TabActivity:

public class OverView extends TabActivity { 

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

     /** TabHost will have Tabs */ 
     TabHost tabHost  = (TabHost)findViewById(android.R.id.tabhost); 

     /** TabSpec used to create a new tab. 
     * By using TabSpec only we can able to setContent to the tab. 
     * By using TabSpec setIndicator() we can set name to tab. */ 

     /** tid1 is firstTabSpec Id. Its used to access outside. */ 
     TabSpec Search  = tabHost.newTabSpec("tid1"); 
     TabSpec AllArtists = tabHost.newTabSpec("tid1"); 
     TabSpec Favorites = tabHost.newTabSpec("tid1"); 
     TabSpec About  = tabHost.newTabSpec("tid1"); 

     /** TabSpec setIndicator() is used to set name for the tab. */ 
     Search.  setIndicator("Search"); 
     AllArtists. setIndicator("AllArtists"); 
     Favorites. setIndicator("Favorites"); 
     About.  setIndicator("About"); 

     /** TabSpec setContent() is used to set content for a particular tab. */ 
     Search.setContent  (new Intent(this, Search.class)); 
     AllArtists.setContent (new Intent(this, AllArtists.class)); 
     Favorites.setContent (new Intent(this, Favorites.class)); 
     About.setContent  (new Intent(this, About.class)); 

     /** Add tabSpec to the TabHost to display. */ 
     tabHost.addTab(Search); 
     tabHost.addTab(AllArtists); 
     tabHost.addTab(Favorites); 
     tabHost.addTab(About); 
    } 
} 

2)所有藝術家(選項卡中的活動。點擊列表項啓動一個新的活動):

public class AllArtists extends Activity { 

    // Debug 
    private final String TAG = this.getClass().getSimpleName(); 

    // XML 
    EditText    searchBox; 
    ListView    listView; 

    // Adapter 
    ListAdapter    listAdapter; 

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

     listAdapter = new ListAdapter(this, null); 

     // XML 
     listView = (ListView)findViewById(R.id.allartists_listview); 
     searchBox = (EditText)findViewById(R.id.allartists_searchbox); 

     listView.setAdapter(listAdapter); 
     listView.setFastScrollEnabled(true); 
     listView.setOnItemClickListener(new OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

       String memberID = (String)listAdapter.getID(position).toString(); 

       if (!memberID.equals("HEADER")){ 
        Log.d(TAG, "Jumping to Artists.class"); 
        Intent intentArtist = new Intent (AllArtists.this, Artist.class); 
        intentArtist.putExtra("ID", memberID); 
        startActivity(intentArtist); 
       } 
      } 
     }); 
} 

3)藝術家(新的活動開始此類不註冊的OnClick):

public class Artist extends Activity implements OnClickListener{ 

// Debug 
private final String TAG = this.getClass().getSimpleName(); 

// XML 
Button   favorite_btn; 

LinearLayout tel; 
LinearLayout mob; 
LinearLayout email; 
LinearLayout www1; 
LinearLayout www2; 
LinearLayout add; 

TextView  name_tv; 
TextView  tel_tv; 
TextView  mob_tv; 
TextView  email_tv; 
TextView  www_tv1; 
TextView  www_tv2; 

// Strings 
String memberID; 
String sirName; 

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

    Toast.makeText(this, "OK, your in Activity_Artist..", Toast.LENGTH_SHORT); 


    // XML 
    favorite_btn = (Button)findViewById(R.id.artist_ib_favorite); 

    tel    = (LinearLayout)findViewById(R.id.artist_tel_container); 
    mob    = (LinearLayout)findViewById(R.id.artist_mob_container); 
    email   = (LinearLayout)findViewById(R.id.artist_email_container); 
    www1   = (LinearLayout)findViewById(R.id.artist_www_container1); 
    www2   = (LinearLayout)findViewById(R.id.artist_www_container2); 
    add    = (LinearLayout)findViewById(R.id.artist_add_container); 

    name_tv   = (TextView)findViewById(R.id.artist_tv_name); 
    tel_tv   = (TextView)findViewById(R.id.artist_tel_dynamic); 
    mob_tv   = (TextView)findViewById(R.id.artist_mob_dynamic); 
    email_tv  = (TextView)findViewById(R.id.artist_email_dynamic); 
    www_tv1   = (TextView)findViewById(R.id.artist_www_dynamic1); 
    www_tv2   = (TextView)findViewById(R.id.artist_www_dynamic2); 

    // OnClickListeners 
    favorite_btn.setOnClickListener(this); 
    tel.setOnClickListener(this); 
    mob.setOnClickListener(this); 
    email.setOnClickListener(this); 
    www1.setOnClickListener(this); 
    www2.setOnClickListener(this); 
    add.setOnClickListener(this); 

     // Code here to get memberID for fillContent() 
} 

private void fillContent(String memberID) throws JSONException { 
// Code here to fill the TextViews etc with content from the DataBase. 
} 

@Override 
public void onClick(View v) { 

    switch (v.getId()){ 

    case R.id.artist_ib_favorite: 
     Toast.makeText(this, "onClick", Toast.LENGTH_SHORT); 
     Log.d(TAG, "Neo is attempting to insert member into favorites"); 

     MyDB db = new MyDB(this); 
     db.insertFavorite(memberID, sirName); 

     break; 

    case R.id.artist_tel_container: 
     break; 

    case R.id.artist_mob_container: 
     Log.d(TAG, "OMG CLICKED THE MOBILE!"); 
     break; 

    case R.id.artist_email_container: 
     break; 

    case R.id.artist_www_container1: 
     break; 

    case R.id.artist_add_container: 
     break; 
    } 
} 

感謝;)

回答

0

解決了這個問題。在佈局中,佈局底部有一個(空的)GridView,設置爲android:layout_height =「fill_parent」,這樣就可以偷走touchevent。

關於這個問題的一個奇怪的部分是,當在選項卡中使用完全相同的XML完全相同的活動時,onClick()工作正常。

0

吐司你需要致電演出。

Toast.makeText(this, "OK, your in Activity_Artist..", Toast.LENGTH_SHORT).show(); 

有關的任何點擊操作除了按鈕,您需要在佈局定義

android:clickable="true" 

+0

Hhaha不是我第一次忘記添加.show()。我實際上從來沒有使用過android:clickable =「true」,而且我從來沒有遇到任何可點擊的問題。不是默認設置爲真? – DecodeGnome 2011-06-16 15:27:15

+0

除此之外,由於這不是TabHost Activity的一部分,因此我沒有看到爲什麼click事件沒有被傳播的任何原因,onClick應該在本活動中被使用。 – PravinCG 2011-06-16 15:48:37