开发者

Problems with OnClickListener for a tab in android

I'm new to android programing and I'm having problems with OnClickListener for tabs in my app. I found on stack a solution how it should be done, but for some reason it's not working.

I'm trying to use the 2nd answer

For some reason I'm get开发者_StackOverflow中文版ting 2 errors.

First one is on the name on of my activity: The type DragonLords must implement the inherited abstract method View.OnClickListener.onClick(View).

Second one is on the OnClick method: The method onClick(View) of type new View.OnClickListener(){} must override a superclass method.

Here is a part of my code:

public class DragonLords extends TabActivity implements OnClickListener{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    Resources res = getResources(); // Resource object to get Drawables
    TabHost tabHost = getTabHost();  // The activity TabHost
    TabHost.TabSpec spec;  // Resusable TabSpec for each tab
    Intent intent;  // Reusable Intent for each tab

    // Create an Intent to launch an Activity for the tab (to be reused)
    intent = new Intent().setClass(this, Home.class);

    // Initialize a TabSpec for each tab and add it to the TabHost
    spec = tabHost.newTabSpec("home").setIndicator("home",
                      res.getDrawable(R.drawable.hometab))
                  .setContent(intent);
    tabHost.addTab(spec);
    getTabWidget().getChildAt(0).setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if (getTabHost().getCurrentTab()==0) {
                getTabHost().setCurrentTab(0);
            }else
            {
                getTabHost().setCurrentTab(0);
            }
        }

    });

After that I'm creating more tabs. With out the onclicklistener it's working, the thing is I need to be able to reload the tabs when they are active.

Anyone have an idea what I'm doing wrong?

I added the necessary imports.

Gatz


  1. You must implement the onClick method not in an anonymous inner class like you have done in your code.
  2. Try using new TabWidget.OnClickListener instead of just the normal OnClickListener

Something akin to the following:

public class TestActivity extends TabActivity implements OnClickListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    getTabWidget().getChildAt(0).setOnClickListener(new TabWidget.OnClickListener() {

        public void onClick(View v) {
            if (getTabHost().getCurrentTab()==0) {
                getTabHost().setCurrentTab(0);
            }else
            {
                getTabHost().setCurrentTab(0);
            }
        }

    });
}

public void onClick(View theView) {
    // Do something with view here

}
}


you are doing it pretty hard way.. i did it like this...

public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.tablayout);

    res = getResources();
    tabHost=(TabHost)this.findViewById(android.R.id.tabhost);

    tabHost.getTabWidget().setDividerDrawable(R.drawable.vertical_seperator);

    setupTab(new TextView(this), "Login",new Intent().setClass(this,loginForm.class));
    setupTab(new TextView(this), "Can't Login",new Intent().setClass(this,ForgotPwd.class));
    setupTab(new TextView(this), "Register",new Intent().setClass(this,RegisterUser.class));
}

private void setupTab(final View view, final String tag,final Intent myIntent) 
{

        View tabview = createTabView(tabHost.getContext(), tag);


      TabSpec setContent = tabHost.newTabSpec(tag).setIndicator(tabview).setContent(
       new TabContentFactory() 
        {
            public View createTabContent(String tag) 
            {return view;}
        }).setContent(myIntent);

      tabHost.addTab(setContent);


}

private static View createTabView(final Context context, final String text)
{
    View view = LayoutInflater.from(context).inflate(R.layout.tabs_bg, null);
    TextView tv = (TextView) view.findViewById(R.id.tabsText);
    tv.setText(text);
    return view;
}

hope this helps.... tabs_bg is just an xml with

<TextView android:id="@+id/tabsText" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:text="Title"
    android:textSize="15dip" 
    android:textColor="@drawable/tab_text_selector" />

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜