2015-07-20 79 views
1

我有延伸LinearLayout一個自定義視圖:如何在Android的自定義視圖中保存實例狀態?

public class CustomEditTextLogin extends LinearLayout { 
    public CustomEditTextLogin(Context context) { 
     super(context); 
     // TODO Auto-generated constructor stub 
     initialize(context); 
    } 
    public CustomEditTextLogin(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     // TODO Auto-generated constructor stub 
     initialize(context); 
    } 
    private LinearLayout llParentCetL; 
    private TextView txtAlertCs; 
    private EditText edt; 
    private ImageView img; 
    public static final int TEXT = 0; 
    public static final int EMAIL = 1; 
    public static final int PASSWORD = 2; 
    public static final int USERNAME = 3; 
    private void initialize(Context context) { 
     LayoutInflater mLayoutInflater = (LayoutInflater) 
     context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View view = mLayoutInflater.inflate(R.layout.custom_edit_text_login, this, true); 
     llParentCetL = (LinearLayout) view.findViewById(R.id.llParentCetL); 
     txtAlertCs = (TextView) view.findViewById(R.id.txtAlertCetL); 
     edt = (EditText) view.findViewById(R.id.edtCetL); 
     img = (ImageView) view.findViewById(R.id.imgCetL); 

     txtAlertCs.setVisibility(View.GONE); 
     int imgMargin = (int) (UIHelpers.width *0.025); 
     UIHelpers.setMargin(img, imgMargin, imgMargin, imgMargin, imgMargin); 
     img.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       setFocus(); 
      } 
     }); 
     public CustomEditTextLogin setFocus(){ 
      if(edt != null){ 
       edt.setFocusableInTouchMode(true); 
       edt.requestFocus(); 

       InputMethodManager imm = (InputMethodManager) App.context.getSystemService(Context.INPUT_METHOD_SERVICE); 
       imm.showSoftInput(edt, InputMethodManager.SHOW_IMPLICIT); 
      } 
      return this; 
     } 
    } 

我要保存實例狀態和恢復它,但它是從活動的實例狀態不同。

如何保存在Android的自定義視圖實例的狀態?

回答

1

不知道我理解正確你。但看起來像你想要獨立於Activity實例保存View的狀態並且能夠恢復它。 您可以爲此使用保留的片段。

  • 擴展Fragment類並聲明對有狀態 對象的引用。
  • 創建片段時調用setRetainInstance(boolean)。
  • 將片段添加到您的活動中。
  • 使用FragmentManager檢索片段時,活動 重新啓動。

    public class RetainedFragment extends Fragment { 
    
    // data object we want to retain 
    private MyDataObject data; 
    
    // this method is only called once for this fragment 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        // retain this fragment 
        setRetainInstance(true); 
    } 
    
    public void setData(MyDataObject data) { 
        this.data = data; 
    } 
    
    public MyDataObject getData() { 
        return data; 
    } 
    

    }

要恢復保存的對象,你可以使用FragmentManager。

public class MyActivity extends Activity { 

    private RetainedFragment dataFragment; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     // find the retained fragment on activity restarts 
     FragmentManager fm = getFragmentManager(); 
     dataFragment = (DataFragment) fm.findFragmentByTag(「data」); 

     // create the fragment and data the first time 
     if (dataFragment == null) { 
      // add the fragment 
      dataFragment = new DataFragment(); 
      fm.beginTransaction().add(dataFragment, 「data」).commit(); 
      // load the data from the web 
      dataFragment.setData(loadMyData()); 
     } 

     // the data is available in dataFragment.getData() 
     ... 
    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     // store the data in the fragment 
     dataFragment.setData(collectMyLoadedData()); 
    } 
} 

在這裏你可以找到有關的詳細信息:http://developer.android.com/guide/topics/resources/runtime-changes.html

希望這有助於。

相關問題