Android - DialogFragment阻止编辑基本活动中的文本焦点

时间:2014-12-01 17:12:11

标签: android listview android-listview android-dialogfragment dialogfragment

美好的一天。

我有一个Android应用程序,我有一个自定义listView。单击customListView后,我更改了所选行的布局(我将最后2列更改为editTexts),并显示了dialogFragment(具有editText)。这是我的代码:

onItemClickListener:

resultListView.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int pos, long id) {

        //first I move the selected row to the top of the listView
        resultListView.smoothScrollToPositionFromTop(pos, 0);

        //then I disable selection of the listView to limit user action
        resultListView.setScrollContainer(false);
        resultListView.setClickable(false);
        resultListView.setEnabled(false);

        //I try to make the row still enabled and clickable
        view.setClickable(true);
        view.setEnabled(true);


        //TODO - set clickstate = 0 is base/default state

        for(SearchResultListViewItem row : results){
            row.setClickState(1); //state 1 = dim
        }

        SearchResultListViewItem rowItem = results.get(pos);
        rowItem.setClickState(2); //state 2 - clicked state

        //in my CustomAdapter, I handle the clickState in the getView method
        //basically, the change happens when click state is 2, the last 2 columns become editText
        adapter.updateResults(results);

        //I removed a lot of unrelated code in here
        ItemDialog itemDialog = new ItemDialog();

        //I set various variables here

        itemDialog.show(getFragmentManager(), "");

    };

});

这是onCreateDialog方法。如下所示,我设置了各种属性,例如setCancelablesetCancelOnTouchOutside和窗口Flags for Modal。

public Dialog onCreateDialog(Bundle savedInstanceState) {  

    final Dialog dialog = new Dialog(getActivity());  

    //set width height here
    int width = getResources().getDisplayMetrics().widthPixels * 4/5;
    int height = getResources().getDisplayMetrics().heightPixels;       
    dialog.getWindow().setLayout(width, height);

    //set other features here
    dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  
            WindowManager.LayoutParams.FLAG_FULLSCREEN);  

    //make sure that the dialog persists through certain events
    dialog.setCancelable(false);
    dialog.setCanceledOnTouchOutside(false);

    LayoutInflater layoutInflater = (LayoutInflater) getActivity()
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View layout2 = layoutInflater.inflate(R.layout.fragment_item_dialog, null);

    dialog.setContentView(layout2);  
    setStyle(DialogFragment.STYLE_NORMAL, R.style.MyDialog);

    //set window properties such as modal, dim, and position
    Window window = dialog.getWindow();
    window.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
            WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
    window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);

    window.setGravity(Gravity.TOP|Gravity.LEFT);

    WindowManager.LayoutParams params = window.getAttributes();
    params.x = 50;
    params.y = y;

    params.copyFrom(window.getAttributes());

    window.setAttributes(params);

    //edited out a lot of extra code

    dialog.show();  


    return dialog;  
}  

如果有必要,我的自定义BaseAdapter中的getView方法中的代码会在我将clickState更改为2时进行处理:

//the if condition checks if the clickstate is < 2, if < 2, do the default and dim state
//click state of 2 means that I use the other row layout which has 2 editTexts 
else{
    convertView = layoutInflater.inflate(R.layout.search_result_inflate, null);
    holder = new ViewHolder();

    holder.itemView = (TextView) convertView.findViewById(R.id.dialogItemName);
    holder.itemView.setText(listData.get(position).getItemName());
    holder.itemView.setBackgroundColor(Color.parseColor("#66B2FF"));

    holder.priceView = (TextView) convertView.findViewById(R.id.price);
    holder.priceView.setText(listData.get(position).getPrice());
    holder.priceView.setBackgroundColor(Color.parseColor("#66B2FF"));

    holder.discountView = (TextView) convertView.findViewById(R.id.discount);
    holder.discountView.setText(listData.get(position).getDiscount());
    holder.discountView.setBackgroundColor(Color.parseColor("#66B2FF"));

    holder.qtyIn = (EditText) convertView.findViewById(R.id.qtyInput);
    holder.qtyIn.setText(listData.get(position).getQtyInput());
    holder.qtyIn.setFocusable(true);
    holder.qtyIn.setClickable(true);

    holder.discIn = (EditText) convertView.findViewById(R.id.discInput);
    holder.discIn.setText(listData.get(position).getDiscInput());
    holder.discIn.setFocusable(true);
    holder.discIn.setClickable(true);

    return convertView;
}

如您所见,我尝试尽可能地启用必要的行。但是,似乎DialogFragment阻止了它。

我也试图评论

        resultListView.setScrollContainer(false);
        resultListView.setClickable(false);
        resultListView.setEnabled(false);

我的OnItemClickListener中的代码块但无济于事。

这个有趣/有趣的是我能够在新行中选择editTexts,我可以看到焦点偏移,但软键盘根本没有出现。仅当我单击DialogFragment中的editText时才会出现软键盘。即使将焦点从对话框Fragment中的EditText切换到列表视图中的那个也不能使我将输入放在行中的EditText中。

如果后台/基本活动中的EditText(行中的那个)与DialogFragment中的EditText之间存在冲突,那么我希望有一种方法可以正确切换焦点,因为我不能去除其中任何一个。

有没有人知道如何解决这个问题?非常感谢任何帮助,谢谢。

编辑:我删除了DialogFragment中的EditText,它仍然无效。

0 个答案:

没有答案