不同选项卡内的按钮

发布于 2025-01-08 10:01:47 字数 2390 浏览 0 评论 0原文

我有三个选项卡,每个选项卡都扩展 Activity 并实现 View.OnClickListener 当我选择某个选项卡并单击按钮时,在 SomeTabActivity 中实现的 OnClickListener 不起作用。为什么? 我需要做什么来创建我的活动? 每次我选择某个选项卡时,我都需要开始新的活动吗?

我的代码:

//主要活动

 public class TripoidActivity extends TabActivity {

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

    private void init() {
         final TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);
         tabHost.setup();
         final Resources res = getResources();

         createTabA(tabHost, res);
         createTabB(tabHost, res);
         createTabC(tabHost, res);

    }

}

//选项卡A活动

 public class TabAActivity extends Activity implements View.OnClickListener {


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
    }

    private void init() {
        buttonAddItem = (Button) findViewById(R.id.btAddItem);
        buttonAddItem.setOnClickListener(TabAActivity.this);
    }

    @Override
    public void onClick(View v) {
        //do something (show a toast msg)
    }

    }

//主要布局xml

 <?xml version="1.0" encoding="utf-8"?>
    <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:padding="5dp" >

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:padding="5dp" >

            <include layout="@layout/tab_a" />
            <include layout="@layout/tab_b" />
            <include layout="@layout/tab_c" />

        </FrameLayout>
    </LinearLayout>
    </TabHost>

I have three tabs and each one extends Activity and implements View.OnClickListener
When I selected some tab and click on a button, the OnClickListener implemented into SomeTabActivity doesn't work. Why?
What I need to do to create my activity?
Every time that I select some tab I need start new activity?

My code:

//the main activity

 public class TripoidActivity extends TabActivity {

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

    private void init() {
         final TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);
         tabHost.setup();
         final Resources res = getResources();

         createTabA(tabHost, res);
         createTabB(tabHost, res);
         createTabC(tabHost, res);

    }

}

//the tab A activity

 public class TabAActivity extends Activity implements View.OnClickListener {


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
    }

    private void init() {
        buttonAddItem = (Button) findViewById(R.id.btAddItem);
        buttonAddItem.setOnClickListener(TabAActivity.this);
    }

    @Override
    public void onClick(View v) {
        //do something (show a toast msg)
    }

    }

//the main layout xml

 <?xml version="1.0" encoding="utf-8"?>
    <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:padding="5dp" >

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:padding="5dp" >

            <include layout="@layout/tab_a" />
            <include layout="@layout/tab_b" />
            <include layout="@layout/tab_c" />

        </FrameLayout>
    </LinearLayout>
    </TabHost>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不即不离 2025-01-15 10:01:47

试试这个...

主要活动

public class TabWidgetExampleActivity extends TabActivity {
/** 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, DailyActivity.class);

    // Initialize a TabSpec for each tab and add it to the TabHost
    spec = tabHost.newTabSpec("Daily").setIndicator("",
                      res.getDrawable(R.drawable.tab_daily))
                  .setContent(intent);
    tabHost.addTab(spec);

    // Do the same for the other tabs
    intent = new Intent().setClass(this, MapActivity.class);
    spec = tabHost.newTabSpec("Map").setIndicator("",
                      res.getDrawable(R.drawable.tab_map))
                  .setContent(intent);
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, HourlyActivity.class);
    spec = tabHost.newTabSpec("Hourly").setIndicator("",
                      res.getDrawable(R.drawable.tab_hour))
                  .setContent(intent);
    tabHost.addTab(spec);


   }
  }

选项卡活动

    public class HourlyActivity extends Activity{
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    final Button btn=new Button(this);
    btn.setText("button");
     final LinearLayout l1=new LinearLayout(this);
    btn.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), "hai", Toast.LENGTH_LONG).show();

            Intent i=new Intent();
            i.setClass(getApplicationContext(), MapActivity.class);
            startActivity(i);


        }
    });
    setContentView(btn);
}

 }

Try This...

Main Activity

public class TabWidgetExampleActivity extends TabActivity {
/** 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, DailyActivity.class);

    // Initialize a TabSpec for each tab and add it to the TabHost
    spec = tabHost.newTabSpec("Daily").setIndicator("",
                      res.getDrawable(R.drawable.tab_daily))
                  .setContent(intent);
    tabHost.addTab(spec);

    // Do the same for the other tabs
    intent = new Intent().setClass(this, MapActivity.class);
    spec = tabHost.newTabSpec("Map").setIndicator("",
                      res.getDrawable(R.drawable.tab_map))
                  .setContent(intent);
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, HourlyActivity.class);
    spec = tabHost.newTabSpec("Hourly").setIndicator("",
                      res.getDrawable(R.drawable.tab_hour))
                  .setContent(intent);
    tabHost.addTab(spec);


   }
  }

Tab Activity

    public class HourlyActivity extends Activity{
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    final Button btn=new Button(this);
    btn.setText("button");
     final LinearLayout l1=new LinearLayout(this);
    btn.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), "hai", Toast.LENGTH_LONG).show();

            Intent i=new Intent();
            i.setClass(getApplicationContext(), MapActivity.class);
            startActivity(i);


        }
    });
    setContentView(btn);
}

 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文