7

你好呀。我不知道发生了什么。我试图EditText在创建 a 时更改 a 的文本DialogFragment,但EditText并没有更新文本。如果我打电话getText().length(),我会注意到内容发生了EditText变化。但是,视觉保持不变,只是空的。

为什么?

预先感谢人们

这是代码:

public class ItemNameDialog extends DialogFragment {
    @Override
    public Dialog onCreateDialog(final Bundle bundle) {
        System.out.println("ON CREATE DIALOG WAS CALLED");   //This appears on LogCat, so this method is called.. the problem is not that

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle("Configure an item to count:");

        LayoutInflater inflater = getActivity().getLayoutInflater();

        View view = inflater.inflate(R.layout.itempick_layout, null);
        builder.setView(view);

        final CheckBox box = (CheckBox) view.findViewById(R.id.itemSenseCheckBox);
        final EditText itemNameBox = (EditText) view.findViewById(R.id.itemNameText);
        final Spinner spinner = (Spinner) view.findViewById(R.id.itemsDefault);
        final int viewIDClicked = getArguments().getInt(clickableViewID);
        final String actualName = getArguments().getString(actualNameItemView);

        System.out.println("H - before: " + itemNameBox.getText().toString().length());    //it appears on logcat "H - before: 0"
        itemNameBox.setText(actualName);
        System.out.println("H - after: " + itemNameBox.getText().toString().length());   //it appears on logcat "H - before: 3"  so why not changing ?

        return builder.create();
    }
}
4

1 回答 1

4

我的问题是,在该代码下方,在 onCreateDialog 方法结束之前,我有一些方法可以控制微调器。
该微调器的第一项具有“不选择任何内容”的目的,在该项目选择中,我只是擦除了该编辑文本的内容,如下所示:

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

if(pos == 0){
    if(editText.length() != 0 )
        editText.setText("");
}

And I didnt know that while creating the spinner, it does trigger the event "onItemSelected" and because of that, the edittext was everytime erased, even when I didnt click on that item of the spinner..

So I managed to overcome this by a simple boolean. Every time the method onCreateDialog I put the boolean to true, and then my onItemSelected just operate when that bolean is false.
Like the code below:

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

    if(firstGoingSpinner){
            firstGoingSpinner = false;

        }else{
            if(pos == 0){
                if(editText.length() != 0 )
                    editText.setText("");

            }else{
                editText.setText(""+parent.getItemAtPosition(pos));
                Editable etext = editText.getText();
                Selection.setSelection(etext, editText.length());
            }

        }
    }


I hope this helps someone in the future ;)

于 2013-03-30T21:00:10.317 回答