【问题标题】:Starting activity in FrameLayout在 FrameLayout 中启动活动
【发布时间】:2012-04-11 14:18:54
【问题描述】:

一旦用户单击在 FrameLayout 中呈现的按钮,我需要启动一个新活动。它呈现了我希望用户单击的按钮,但当然它现在没有做任何事情。

类的代码如下,但是不能调用startActivity(intent)。

public class TopBarView extends FrameLayout {

    private ImageView mLogoImage;
    private Button mInfoButton;

    public TopBarView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public TopBarView(Context context) {
        super(context);
        init();
    }

    public TopBarView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.top_bar, null);

        mLogoImage = (ImageView) view.findViewById(R.id.imageLogo);
        mInfoButton = (Button) view.findViewById(R.id.infoButton);

        mInfoButton.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // We load & render the view for the information screen
//              Intent i = new Intent();
//              i.setClass(getContext(), MeerActivity.class);
//              startActivity(i);
            }
        });

        addView(view);
    }
}

提前非常感谢!

【问题讨论】:

    标签: android android-intent android-activity android-framelayout


    【解决方案1】:

    改变:

    public void onClick(View v) {
    // We load & render the view for the information screen
    //              Intent i = new Intent();
    //              i.setClass(getContext(), MeerActivity.class);
    //              startActivity(i);
    }
    

    收件人:

    public void onClick(View v) {
    // We load & render the view for the information screen
        Intent i = new Intent();
        i.setClass(v.getContext(), MeerActivity.class);
        v.getContext().startActivity(i);
    }
    

    注意:通过您正在使用的活动分配 onclicklistener 可能会更好,因此 TopBarView 更可重用,以防您想使用 MeerActivity 以外的其他东西作为目标。没什么大不了的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-10
      • 1970-01-01
      • 2023-03-04
      • 1970-01-01
      • 2014-01-12
      相关资源
      最近更新 更多