标签:mit ati hid activity android 效果 wrap ota textview
1.创建Crime实体类
package com.criminalintent; import java.util.UUID; /** * Created by yeeson on 2015/11/24. */ public class Crime { private UUID uuid; private String title; public Crime(){ uuid=UUID.randomUUID(); } public UUID getUuid() { return uuid; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } }
package com.criminalintent; import java.util.UUID; /** * Created by yeeson on 2015/11/24. */ public class Crime { private UUID uuid; private String title; public Crime(){ uuid=UUID.randomUUID(); } public UUID getUuid() { return uuid; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } }
2.定义容器视图activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/fragmentContainer" android:layout_width="match_parent" android:layout_height="match_parent"> </FrameLayout>
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/fragmentContainer" android:layout_width="match_parent" android:layout_height="match_parent"> </FrameLayout>
3.定义CrimeFragment的布局视图
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <EditText android:id="@+id/crime_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/crime_title_hint"/> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <EditText android:id="@+id/crime_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/crime_title_hint"/> </LinearLayout>
4.创建CrimeFragment类
package com.criminalintent; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.text.Editable; import android.text.TextWatcher; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; /** * Created by yeeson on 2015/11/24. */ public class CrimeFragment extends Fragment { private Crime crime; TextView titleFiled; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); crime=new Crime(); } @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view=inflater.inflate(R.layout.fragment_crime,container,false); titleFiled= (TextView) view.findViewById(R.id.crime_title); titleFiled.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { crime.setTitle(charSequence.toString()); } @Override public void afterTextChanged(Editable editable) { } }); return view; } }
package com.criminalintent; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.text.Editable; import android.text.TextWatcher; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; /** * Created by yeeson on 2015/11/24. */ public class CrimeFragment extends Fragment { private Crime crime; TextView titleFiled; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); crime=new Crime(); } @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view=inflater.inflate(R.layout.fragment_crime,container,false); titleFiled= (TextView) view.findViewById(R.id.crime_title); titleFiled.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { crime.setTitle(charSequence.toString()); } @Override public void afterTextChanged(Editable editable) { } }); return view; } }
5.在MainActivity 中动态添加CrimeFragment
package com.criminalintent; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); FragmentManager fm=getSupportFragmentManager(); Fragment fragment=fm.findFragmentById(R.id.fragmentContainer); if(fragment==null){ fragment=new CrimeFragment(); fm.beginTransaction() .add(R.id.fragmentContainer, fragment) .commit(); } } }
package com.criminalintent; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); FragmentManager fm=getSupportFragmentManager(); Fragment fragment=fm.findFragmentById(R.id.fragmentContainer); if(fragment==null){ fragment=new CrimeFragment(); fm.beginTransaction() .add(R.id.fragmentContainer, fragment) .commit(); } } }
最后在模拟其中运行效果如下:
标签:mit ati hid activity android 效果 wrap ota textview
原文地址:http://www.cnblogs.com/zhuzhangy/p/7616051.html