我有一个带有4个标签的TabActivity。单击其中一个选项卡中的按钮并启动新活动(不在TabHost中的新活动)时,新活动不会注册OnClick()。新的活动甚至无法显示Toast,这让我觉得TabHost会以某种方式阻止ui?
当将Activity作为选项卡之一时,OnClick的工作正常。 任何想法是什么原因?
我已经包含了3个课程:
1)TabActivity
2)开始的标签中的活动:
3)无法注册OnClick()
的新Activity1)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)AllArtists(选项卡中的活动。点击列表项会启动一个新活动):
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 :(得分:0)
对于Toast,您需要调用show。
Toast.makeText(this, "OK, your in Activity_Artist..", Toast.LENGTH_SHORT).show();
对于除Button之外的任何内容的单击操作,您需要定义
android:clickable="true"
在布局中。
答案 1 :(得分:0)
解决了这个问题。在布局中,布局底部有一个(空)GridView设置为android:layout_height =“fill_parent”,它偷走了touchevent。
关于这一点的奇怪之处在于,当在选项卡中放置完全相同的活动和完全相同的XML时,onClick()工作正常。