android - 在 FrameLayout 中启动 Activity

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

一旦用户单击在 FrameLayout 中呈现的按钮,我需要启动一个新 Activity 。它呈现了我希望用户单击的按钮,但当然它现在没有执行任何操作。

该类的代码如下,但我无法调用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);
    }
}

提前非常感谢!

最佳答案

更改:

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

注意:通过您正在使用的 Activity 分配 onclicklistener 可能会更好,这样,如果您想使用 MeerActivity 以外的其他内容作为目标,TopBarView 的可重用性更高。没什么大不了的。

关于android - 在 FrameLayout 中启动 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10107811/

相关文章:

android - 单击android通知图标不会触发广播接收器

java - 将字符串从一个类发送到一个 Activity

android - 在 Android M 开发者预览版中找不到 PackageInstallerActivity

android - 从服务调用时 Activity 重新启动

java - Java List 的高效循环

android - Android 服务中的 SharedPreferences

android - Gradle构建错误

android - 多种 MIME 类型的 Intent 过滤器

android - 将 Fragment 中的 Parcelable 对象共享到 Activity

android - 如何以编程方式使用 ColorStateList 设置 FloatingActionButton 的 backgroundTint?