标题栏中的按钮如何到达

发布于 2024-12-28 19:13:44 字数 1403 浏览 0 评论 0原文

创建带有按钮的标题栏时,这在所有活动中都很常见,例如在选项卡活动中创建的标题栏。如何才能到达所有子活动中的按钮?

public class tabActivity extends TabActivity  implements OnClickListener{
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState);
    c = this;
    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

    setContentView(R.layout.tabactivity);

    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);

    mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("Settings",
            res.getDrawable(R.drawable.preferences)).setContent(
                    new Intent(this, Settings.class)));

    mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("About",
            res.getDrawable(R.drawable.newspaper)).setContent(
                    new Intent(this, About.class)));

这是我初始化选项卡和带有按钮的自定义标题的地方。

在这个类中,我想访问自定义标题中的按钮。:

public class About extends Activity 
{
@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);

    setContentView(R.layout.about);

    ImageView imag = (ImageView) findViewById(R.id.Position);
    imag.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            System.out.println("heeey");
        }
    });

}

侦听器不起作用?

呼这可能吗?

When creating a titlebar with a button, which is common in all activities e.g. title bar created in tabactivities. how is it possible to reach the button in all of the sub activities??

public class tabActivity extends TabActivity  implements OnClickListener{
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState);
    c = this;
    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

    setContentView(R.layout.tabactivity);

    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);

    mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("Settings",
            res.getDrawable(R.drawable.preferences)).setContent(
                    new Intent(this, Settings.class)));

    mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("About",
            res.getDrawable(R.drawable.newspaper)).setContent(
                    new Intent(this, About.class)));

This is here where i initialize my tabs, and the custom title with buttons..

And in this class i would like to reach the buttons in the custom title.:

public class About extends Activity 
{
@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);

    setContentView(R.layout.about);

    ImageView imag = (ImageView) findViewById(R.id.Position);
    imag.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            System.out.println("heeey");
        }
    });

}

The listener doesnt work??

Hooow is this possible??

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

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

发布评论

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

评论(2

我的奇迹 2025-01-04 19:13:44

像这样创建一个抽象的Activity

public abstract class MyAbsActivity extends Activity {
    abstract public void buttonClickedInTab();
}

然后使用MyAbsActivity扩展选项卡的两个子Activity,然后你必须重写buttonClickedInTab()方法。

在重写该方法时,将您的逻辑放在两个活动中。例如,带有 buttonClickedInTab()About 应该如下所示:

public class About extends MyAbsActivity {

    //onCreate() etc here

    @Override
    public void buttonClickedInTab() {
        //Your logic to do action for About
    }

}

对其他 SettingActivity 重复此操作。

现在在您的 TabActivity 中处理标题按钮事件 onClick()

public void onClick(View v)
{
    MyAbsActivity activity = (MyAbsActivity)getLocalActivityManager().getCurrentActivity();
    activity.buttonClickedInTab(); // it will inform the current activity
}

Make an abstract Activity like this:

public abstract class MyAbsActivity extends Activity {
    abstract public void buttonClickedInTab();
}

Then extend your both child Activity of tabs with MyAbsActivity, then you have to override buttonClickedInTab() method.

While overriding the method put your logic there for both Activities. for example your About with buttonClickedInTab() should look like:

public class About extends MyAbsActivity {

    //onCreate() etc here

    @Override
    public void buttonClickedInTab() {
        //Your logic to do action for About
    }

}

Repeat this for other SettingActivity.

Now in your TabActivity where you handle the title button event onClick()

public void onClick(View v)
{
    MyAbsActivity activity = (MyAbsActivity)getLocalActivityManager().getCurrentActivity();
    activity.buttonClickedInTab(); // it will inform the current activity
}
榕城若虚 2025-01-04 19:13:44
public class Android_templateActivity extends Activity
{
private static OnClickListener listener;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    View v = new View(this);

    v.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            Android_templateActivity.listener.onClick(v);
        }
    });
}

public static void setListener(View.OnClickListener listener)
{
    Android_templateActivity.listener = listener;
}
}

这是主要活动

public class aaa extends Activity implements OnClickListener
{
@Override
protected void onResume()
{
    Android_templateActivity.setListener(this);
    super.onResume();
}

@Override
public void onClick(View v)
{
    // TODO Auto-generated method stub

}
}

这是子活动

public class Android_templateActivity extends Activity
{
private static OnClickListener listener;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    View v = new View(this);

    v.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            Android_templateActivity.listener.onClick(v);
        }
    });
}

public static void setListener(View.OnClickListener listener)
{
    Android_templateActivity.listener = listener;
}
}

this is main activity

public class aaa extends Activity implements OnClickListener
{
@Override
protected void onResume()
{
    Android_templateActivity.setListener(this);
    super.onResume();
}

@Override
public void onClick(View v)
{
    // TODO Auto-generated method stub

}
}

this is subactivity

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