2011-06-16 60 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