我正在尝试创建一个片段,在显示时,根据我传递给show()
方法的额外项目更改其内容。保留对话框片段实例以保证可重用性,换句话说,我只是实例化片段一次并使用它显示的新对象调用show()
。它第一次显示时效果很好,但是对于后续的show调用,我只是无法更改编辑文本的文本。您将在下面的课程中看到我实际上明确地将文本更改为show()
代码块之后的“我已被更改”,但这绝不会在ui中反映出来。
public class RegisterFragment extends DialogFragment implements View.OnClickListener, DialogInterface.OnClickListener
{
private static final String KEY_SOURCE = "source";
private static String source;
private static JSONObject listEntry;
private RegisterItem item;
private boolean spinnerMode;
private boolean viewsCreated;
private TextView label1, label2;
private EditText field1 = null, field2 = null;
private Spinner spinner;
private ArrayAdapter<String> adapter;
private Button send, cancel;
private OnClickSendCancelButtonListener sendListener;
public static RegisterFragment newInstance(String source)
{
RegisterFragment fragment = new RegisterFragment();
Bundle b = new Bundle();
b.putString(KEY_SOURCE, source);
fragment.setArguments(b);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Bundle b = this.getArguments();
if (b != null)
{
source = b.getString(KEY_SOURCE);
load(source);
}
adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
viewsCreated = false;
}
public RegisterItem getRegisterItem()
{
return this.item;
}
@Override
public void onAttach(Activity activity)
{
super.onAttach(activity);
try
{
this.sendListener = (OnClickSendCancelButtonListener) activity;
}
catch (ClassCastException e)
{
throw new ClassCastException("Activity must implement RegisterFragment#OnClickSendCancelButtonListener");
}
}
private void load(String source)
{
if (item == null)
item = new RegisterItem();
item.loadData(source);
spinnerMode = item.hasChoices();
if (!viewsCreated)
return;
label1.setText(item.field1Label);
label2.setText(item.field2Label);
send.setText(item.sendButtonText);
cancel.setText(item.cancelButtonText);
Log.v("RegisterFragment", "FIELD1: " + item.defaultField1);
Log.v("RegisterFragment", "FIELD2: " + item.defaultField2);
if (spinnerMode)
{
field1.setVisibility(View.GONE);
spinner.setVisibility(View.VISIBLE);
adapter.clear();
for(String choice: item.choices)
adapter.add(choice);
adapter.notifyDataSetChanged();
}
else
{
field1.setVisibility(View.VISIBLE);
spinner.setVisibility(View.GONE);
}
if (item.hasDefaults())
{
Log.v("RegisterFragment", "Setting text for field 2");
field2.setText(item.defaultField2);
Log.v("RegisterFragment", "FIELD2 TEXT: " + field2.getText().toString());
if (spinnerMode)
spinner.setSelection(adapter.getPosition(item.defaultField2));
else
field1.setText(item.defaultField1);
}
}
public void show(FragmentManager manager, String tag, String source, String listEntry)
{
Log.v("Register Fragment", "Showing fragment\nSOURCE STRING: " + source);
load(source);
RegisterFragment.source = source;
try {
RegisterFragment.listEntry = new JSONObject(listEntry);
} catch (JSONException e) {
e.printStackTrace();
}
super.show(manager, tag);
if (field2 != null)
field2.setText("I Have Been Changed");
}
@Override
public void dismiss()
{
if (field1 != null)
field1.setText("");
if (field2 != null)
field2.setText("");
super.dismiss();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View v = inflater.inflate(R.layout.register_screen, null);
spinner = (Spinner) v.findViewById(R.id.register_spinner);
spinner.setAdapter(adapter);
label1 = (TextView) v.findViewById(R.id.register_label1);
label2 = (TextView) v.findViewById(R.id.register_label2);
field1 = (EditText) v.findViewById(R.id.register_field1);
field2 = (EditText) v.findViewById(R.id.register_field2);
send = (Button) v.findViewById(R.id.register_send);
cancel = (Button) v.findViewById(R.id.register_cancel);
send.setOnClickListener(this);
cancel.setOnClickListener(this);
viewsCreated = true;
load(source);
return v;
}
@Override
public void onDestroyView() {
super.onDestroyView();
viewsCreated = false;
}
}
答案 0 :(得分:0)
您的代码应该有效。但是试试这个。
EditText text = (EditText)getDialog().findViewById(R.id.register_field2);
text.setText("I have been changed");