标题栏中的按钮如何到达它

时间:2012-01-26 03:01:30

标签: android tabactivity titlebar

使用按钮创建标题栏时,这在所有活动中都很常见,例如在tabactivities中创建的标题栏。如何在所有子活动中到达按钮?

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");
        }
    });

}

听众不起作用?

Hooow这可能吗?

2 个答案:

答案 0 :(得分:3)

像这样制作一个抽象的Activity

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

然后使用Activity扩展您的两个子标MyAbsActivity,然后您必须覆盖buttonClickedInTab()方法。

虽然覆盖该方法,但在两个活动中都有你的逻辑。例如,About buttonClickedInTab()的{​​{1}}应如下所示:

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
}

答案 1 :(得分:2)

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

}
}

这是子活动