PopupWindow不会长按EditText上的sytem上下文对话框

时间:2012-11-06 09:58:56

标签: android popup window android-edittext

很抱歉,如果标题有点模糊。

我正在为Freelancer开发一款应用程序,除了经过一些测试后客户的投诉外,我几乎完成了它。

我使用PopupWindow代替对话框来编辑上下文设置,如果这有意义的话。我不想过于具体,并且不愿意放弃应用程序概念,我相信客户不会太高兴。

PopupWindow被赋予了从XML扩展的布局的内容视图。在那个布局中有几个EditText小部件。问题是那些EditTexts不会在长按时触发默认的上下文对话框,它提供文本/ IME选择和剪切/复制/粘贴的选项。

我看到一个类似的问题试图获取TouchTrigger或其他东西,如果没有setBackgroundDrawable(),我已经尝试使用一个简单的新ColorDrawable()。它仍然无效。

有没有简单的方法可以在OnLongPressListener中触发系统默认的长按对话框,还是我必须移动天堂和地球来实现它?因为如果是这种情况,我只会为它编写一个Fragment并在事务中将其交换出来。我知道那会有用。

相关代码: 在初始片段内:

RulesDialog dialog;
PopupWindow window;

public void showAddRuleDialog(){
    dialog = new RulesDialog();

    View view = getView();

    window = new PopupWindow(dialog.initViews(this, null), view.getWidth(), view.getHeight(), true);

    window.setBackgroundDrawable(new ColorDrawable());

    dialog.setRulesDialogListener(new rulesDialogListener(){            

        @Override
        public void onSave(ViewHolder holder) {


            addRule(holder);

            window.dismiss();


        }

        @Override
        public void onCancel() {
            window.dismiss();

        }});

    int[] location = {0,0};
    view.getLocationOnScreen(location);



    window.showAtLocation(view, 0, location[0], location[1]);

在RulesDialog中:

public class ViewHolder{
    public ViewHolder(View dialogView){
        name = (TextView) dialogView.findViewById(R.id.name);
        response = (TextView) dialogView.findViewById(R.id.response);

        senders = (TextView) dialogView.findViewById(R.id.senders);
        sendersAdd = (Button) dialogView.findViewById(R.id.sendersAdd);
        sendersEdit = (Button) dialogView.findViewById(R.id.sendersEdit);

        timeFrom = (TextView) dialogView.findViewById(R.id.from);
        timeFromEdit = (Button) dialogView.findViewById(R.id.timeBeforeEdit);
        timeTo = (TextView) dialogView.findViewById(R.id.to);
        timeToEdit = (Button) dialogView.findViewById(R.id.timeAfterEdit);

        keywords = (TextView) dialogView.findViewById(R.id.keywords);

        matchCase = (CheckBox) dialogView.findViewById(R.id.matchCase);
        matchAlone = (CheckBox) dialogView.findViewById(R.id.matchAlone);
        matchPlural = (CheckBox) dialogView.findViewById(R.id.matchPlural);

        cancel = (Button) dialogView.findViewById(R.id.cancel);
        save = (Button) dialogView.findViewById(R.id.save);


    }

    TextView name;
    TextView response;
    TextView senders;
    Button sendersAdd;
    Button sendersEdit;
    TextView timeFrom;
    Button timeFromEdit;
    TextView timeTo;
    Button timeToEdit;
    TextView keywords;

    CheckBox matchCase;
    CheckBox matchAlone;
    CheckBox matchPlural;

    Button cancel;
    Button save;


}

Activity activity;
ViewHolder holder;
Fragment fragment;



public View initViews(Fragment mFragment, Rule rule){
    fragment = mFragment;
    activity = fragment.getActivity();
    View dialogView = LayoutInflater.from(activity).inflate(R.layout.rules_dialog, null);

    holder = new ViewHolder(dialogView);

    final TextView senders = holder.senders;

    holder.sendersAdd.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {
            showContacts();
        }});

    holder.sendersEdit.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {
            editSenders(senders);

        }

    });

    final TextView timeFrom = holder.timeFrom;
    holder.timeFromEdit.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {
            showTimePickerDialog(timeFrom);             

        }

        });

    final TextView timeTo = holder.timeTo;
    holder.timeToEdit.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {
            showTimePickerDialog(timeTo);               

        }

        });


    holder.cancel.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {
            mListener.onCancel();

        }});        
    holder.save.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {
            mListener.onSave(holder);

        }});


    if(rule == null)
        rule = new Rule();


    holder.name.setText(rule.name);
    holder.response.setText(rule.response);

    holder.senders.setText(rule.senders.toString());
    holder.senders.setTag(rule.senders);                

    holder.keywords.setText(rule.keywords);

    holder.matchCase.setChecked(rule.matchCase);
    holder.matchAlone.setChecked(rule.matchAlone);
    holder.matchPlural.setChecked(rule.matchPlural);        

    holder.timeFrom.setTag(rule.timeFrom);                          
    holder.timeFrom.setText(Rules.formatTime(rule.timeFrom));

    holder.timeTo.setTag(rule.timeTo);
    holder.timeTo.setText(Rules.formatTime(rule.timeTo));

    return dialogView;
}

1 个答案:

答案 0 :(得分:0)

所以我尝试将RulesDialog重写为片段,但效果不佳。当从他们正在操作的碎片调用时,使碎片事务处理正常工作存在问题。

(我知道这不是片段的重点。我现在并不是真的想要编写一个完全模块化的应用程序。我只想提出一个客户会满意的产品。)

我最终将RulesDialog重写为Activity,并使用调用片段中的startActivityForResult()。然后使用setResult()传回已编辑的数据。这一切都很好地协调。