简体   繁体   English

在首选项行中添加一个按钮

[英]Add a button in Preference Row

I want a button in my Settings screen.我想要在我的设置屏幕上有一个按钮。 This exact question has been asked here .这个确切的问题已被问到here But unfortunately has no answers.但不幸的是没有答案。 :( :(

在此处输入图片说明

In order to get to this, I created a custom preference like this -为了做到这一点,我创建了一个这样的自定义首选项 -

  public class CustomPreference extends Preference {

    private LinearLayout mWidgetContainer;
    private View mRowView;

    public CustomPreference(Context context) {
        super(context);
    }

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

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

    @Override
    protected View onCreateView(ViewGroup parent) {
        LayoutInflater viewInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        mRowView = viewInflater.inflate(R.layout.preferences_row_view, parent, false);

        mWidgetContainer = (LinearLayout) mRowView.findViewById(android.R.id.widget_frame);

        Button button = new Button(getContext());
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

        button.setLayoutParams(params);
        button.setBackgroundResource(R.drawable.listview_row_bg);
        button.setTextSize(14);
        button.setPadding(5, 5, 5, 5);
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), BuyScreenActivity.class);
                getContext().startActivity(intent);
            }
        });
        button.setTypeface(null, Typeface.BOLD);
        button.setText("Buy now");
        mWidgetContainer.addView(button);

        return mRowView;
    }
}

This does work.这确实有效。 But it behaves strange.但它的行为很奇怪。 As you can see on click of that button I'm taking the user to Activity called BuyScreenActivity .正如您在单击该按钮时所看到的,我将用户带到名为BuyScreenActivity Activity 。 The strange part is when I press back on BuyScreenActivity , I come back to my Settings screen but onDestroy and onStop of BuyScreenActivity is not called at all.奇怪的是,当我按下BuyScreenActivity ,我回到了我的设置屏幕,但BuyScreenActivity没有调用BuyScreenActivity onDestroyonStop Why would it behave that way?为什么会这样呢?

If I scroll down the settings screen, onStop & onDestroy will then be called.如果我向下滚动设置屏幕,则会调用onStoponDestroy Why does this have to behave that way?为什么必须这样做?

I don't know why that is happening exactly, but if it bothers you, just override onBackPressed() in BuyScreenActivity.java:我不知道为什么会发生这种情况,但如果它困扰你,只需覆盖 BuyScreenActivity.java 中的 onBackPressed() :

@Override
public void onBackPressed() {
    startActivity(new Intent(getContext(), SettingsACtivity.class));
}

Note that I'm assuming that you're only getting to BuyScreen through Settings.请注意,我假设您只能通过设置进入 BuyScreen。 If you're not, then it's easy enough to put the name of the previous activity into the intent data, and switch that to get to the desired activity.如果不是,那么很容易将前一个活动的名称放入意图数据中,然后将其切换到所需的活动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM