在静态 class 中保存以编程方式生成的 edittext 值

saving programatically generated value of edittext in a static class

我在选项卡中有一个 edittext 字段,它在创建 activity 时被赋予一个唯一的自动生成值。我希望在将值放入编辑文本后立即将此值传递给静态 class,以便在切换选项卡时,可以从静态 class 调用该值。 我的问题是我尝试使用以下代码将值保存到静态 class:

 invoiceNo.addTextChangedListener(new TextWatcher() {
        public void afterTextChanged(Editable s) {
            //GlobalApp.data().id = invoiceNo.getText().toString();
        }

        public void beforeTextChanged(CharSequence s, int a, int b, int c){
            GlobalApp.data().id = invoiceNo.getText().toString();
        }

         public void onTextChanged(CharSequence s, int a, int b, int c){
            // GlobalApp.data().id = invoiceNo.getText().toString();
         }
        }

但是每当用户 returns 到第一个选项卡(生成值的地方)时,它就会不断变化,否则它根本不会显示。我希望它保存生成的值一次,而不管用户 returns 是否到第一个选项卡。

public class clientFragmentTab  extends Fragment  {

ArrayList<String> saleRecord;
Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
String formattedDate = df.format(c.getTime());
EditText currDate, invoiceNo , vehicle,territory ;
//Spinner clientName, territory;
View rootView = null;
int invoice_id = 0;
String invoice_no;
public String[] item = new String[] {"Please search..."};
public String territory1;
CustomAutoCompleteView myAutoComplete;
ProductsDbHelper db;

// adapter for auto-complete
ArrayAdapter<String> myAdapter;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    rootView = inflater.inflate(R.layout.client_layout, container, false);



    return rootView;
}

public void onActivityCreated(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onActivityCreated(savedInstanceState);
    myAutoComplete = (CustomAutoCompleteView) rootView.findViewById(R.id.myautocomplete);

    // add the listener so it will tries to suggest while the user types
    myAutoComplete.addTextChangedListener(new CustomAutoCompleteTextChangedListener(this, getActivity()));

    db = new ProductsDbHelper(getActivity());
    // set our adapter
    myAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_dropdown_item_1line, item);
    myAutoComplete.setAdapter(myAdapter);
    currDate = (EditText) rootView.findViewById(R.id.editText4);
    //clientName = (Spinner) rootView.findViewById(R.id.spinner);
    invoiceNo = (EditText) rootView.findViewById(R.id.editText3);
    vehicle = (EditText) rootView.findViewById(R.id.editText6);
    territory = (EditText) rootView.findViewById(R.id.editText9);invoice_id = UniqueRandomNumbers();


    invoice_no = "invoice_" + invoice_id;

    currDate.setText(formattedDate);
    invoiceNo.setText(invoice_no);




    //List<String> list = new ArrayList<String>();
    //list.add("Select Client");
    //list.add("Item 2");
    //list.add("Item 3");
    //list.add("Item 4");
    //list.add("Item 5");

    //ArrayAdapter<String> adapter = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_dropdown_item , list);
    //adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    //clientName.setAdapter(adapter);





   // if(invoiceNo.getText().toString() != ""){

   // }

    invoiceNo.addTextChangedListener(new TextWatcher() {
        public void afterTextChanged(Editable s) {
            //GlobalApp.data().id = invoiceNo.getText().toString();
        }

        public void beforeTextChanged(CharSequence s, int a, int b, int c){
            GlobalApp.data().id = invoiceNo.getText().toString();
        }

         public void onTextChanged(CharSequence s, int a, int b, int c){
            // GlobalApp.data().id = invoiceNo.getText().toString();
         }
        }

        );
}

您似乎完全错误地处理了这一切,当您觉得某些事情必须强制执行时,通常表示项目架构存在问题。解决方案应该类似于以下内容:

1. onCreate generate that id and store it in a *member variable*, lets call it mInvoiceId
2. onCreateView or onViewCreated do invoiceNo.setText(mInvoiceId)
3. if it's still required, in onTextChanged do mInvoiceId = s; (the 's' param should be the same result as invoiceNo.getText().toString() I believe)
4. if you're really set on using a static class you can replace any use of mInvoiceId  with GlobalApp.data().id but static classes are usually bad. 
   - You can pass things between fragments correctly using saveInstanceState or intents with extras (not sure what you want to do)

我希望这能让你朝着正确的方向前进