fragment onCreate和onCreateView的区别

onCreate是指创建该fragment类似于Activity.onCreate,

你可以在其中初始化除了view之外的东西,onCreateView是创建该fragment对应的视图

,你必须在这里创建自己的视图并返回给调用者,例如

return inflater.inflate(R.layout.fragment_settings, container, false);。


	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_status, container,false);
		
		editStatus=(EditText)view.findViewById(R.id.editStatus);
		buttonTweet=(Button)view.findViewById(R.id.buttonTweet);
		textCount=(TextView)view.findViewById(R.id.textCount);
		
		defaultTextColor = textCount.getTextColors().getDefaultColor();
		buttonTweet.setOnClickListener(this);
		
		editStatus.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				int count = 140-editStatus.length();
				textCount.setText(Integer.toString(count));
				textCount.setTextColor(Color.GREEN);
				if(count<10){
					textCount.setTextColor(Color.RED);
				}else{
					textCount.setTextColor(defaultTextColor);
				}
			}
		});
		return view;
	}


你可能感兴趣的:(fragment onCreate和onCreateView的区别)