您的位置:首页 > Web前端

09-07 Fragment(碎片续)、数据存储(Preference)

2015-09-07 21:35 513 查看
Fragment仿QQ,实现滑动

//**activity_main**
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_weight="1">

</android.support.v4.view.ViewPager>

<FrameLayout
android:id="@+id/framelayout"
android:layout_width="match_parent"
android:layout_height="match_parent"></FrameLayout>

<RadioGroup
android:id="@+id/radiogroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center"
android:orientation="horizontal">
<RadioButton
android:id="@+id/radionutton_msg"
style="@style/RadioButtonStyle"
android:drawableTop="@drawable/conversation" />
<RadioButton
android:id="@+id/radionutton_con"
style="@style/RadioButtonStyle"
android:drawableTop="@drawable/contact" />
<RadioButton
android:id="@+id/radionutton_plu"
style="@style/RadioButtonStyle"
android:drawableTop="@drawable/plugin" />
</RadioGroup>
</RelativeLayout>

//**MyFragmentAdapter**
public class MyFragmentAdapter extends FragmentPagerAdapter{
private List<Fragment> mFragments;
public MyFragmentAdapter(FragmentManager fm,List<Fragment> fragments) {
super(fm);
mFragments=fragments;
}

@Override
public Fragment getItem(int position) {
return mFragments.get(position);
}

@Override
public int getCount() {
return mFragments.size();
}
}

//**MainActivity**
public class MainActivity extends FragmentActivity {
private FrameLayout mFrameLayout;
private MyFirstFragment myFirstFragment;
private MySecondFragment mySecondFragment;
private MyThirdFragment myThirdFragment;
private FragmentManager mFragmentManager;
private FragmentTransaction mFragmentTransaction;
private RadioGroup mRadioGroup;
private ViewPager mViewPager;
private List<Fragment> mFragments;
private MyFragmentAdapter mAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//        mFrameLayout= (FrameLayout) findViewById(R.id.framelayout);
mFragmentManager=getSupportFragmentManager();//FragmentManager的初始化
mRadioGroup= (RadioGroup) findViewById(R.id.radiogroup);
mViewPager= (ViewPager) findViewById(R.id.viewpager);
mFragments=new ArrayList<>();
//三个tFragment的初始化
myFirstFragment=new MyFirstFragment();
mySecondFragment=new MySecondFragment();
myThirdFragment=new MyThirdFragment();
mFragments.add(myFirstFragment);
mFragments.add(mySecondFragment);
mFragments.add(myThirdFragment);
mAdapter=new MyFragmentAdapter(mFragmentManager,mFragments);
mViewPager.setAdapter(mAdapter);
mRadioGroup.check(R.id.radionutton_msg);
mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId){
case R.id.radionutton_msg:
mViewPager.setCurrentItem(0);
break;
case R.id.radionutton_con:
mViewPager.setCurrentItem(1);
break;
case R.id.radionutton_plu:
mViewPager.setCurrentItem(2);
break;
default:
break;
}
}
});
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

}

@Override
public void onPageSelected(int position) {
switch (position){
case 0:
mRadioGroup.check(R.id.radionutton_msg);
break;
case 1:
mRadioGroup.check(R.id.radionutton_con);
break;
case 2:
mRadioGroup.check(R.id.radionutton_plu);
break;
default:
break;
}
}

@Override
public void onPageScrollStateChanged(int state) {

}
});
}
}
}


数据存储(Preference)

//**activity_main.xml**
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"  tools:context=".MainActivity"
android:orientation="vertical">
<EditText
android:id="@+id/edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/button_write"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="写入信息"/>
<Button
android:id="@+id/button_read"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="读取信息"/>
<Button
android:id="@+id/button_write_cache"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="写入缓存信息"/>
<Button
android:id="@+id/button_read_cache"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="读取缓存信息"/>
<Button
android:id="@+id/button_cache_dir"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="缓存DIR"/>
<Button
android:id="@+id/button_sdcard"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="本地磁盘"/>
</LinearLayout>

//**AndroidManifest.xml**
//在sdcard中操作数据时需要权限
<!--外部拓展卡读取权限-->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<!-- 本地磁盘读取权限-->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

//**MainActivity**
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private TextView mTextView;
private EditText mEditText;
private Button mButton_write;
private Button mButton_read;
private Button mButton_write_cache;
private Button mButton_read_cache;
private Button mButton_cache_dir;
private Button mButton_sdcard;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mTextView= (TextView) findViewById(R.id.textview);
mEditText= (EditText) findViewById(R.id.edittext);

mButton_read= (Button) findViewById(button_read);
mButton_write= (Button) findViewById(R.id.button_write);
mButton_write_cache= (Button) findViewById(R.id.button_write_cache);
mButton_read_cache= (Button) findViewById(R.id.button_read_cache);
mButton_cache_dir= (Button) findViewById(R.id.button_cache_dir);
mButton_sdcard= (Button) findViewById(R.id.button_sdcard);

mButton_read.setOnClickListener(this);
mButton_write.setOnClickListener(this);
mButton_write_cache.setOnClickListener(this);
mButton_read_cache.setOnClickListener(this);
mButton_cache_dir.setOnClickListener(this);
mButton_sdcard.setOnClickListener(this);
}

@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button_write:
WriteToPreference();//向Preference中写入数据
break;
case R.id.button_read:
ReadFromPreference();//从Preference中写入数据
break;
case R.id.button_write_cache:
writeToCache("HelloWord.tex");//向files中写入数据
break;
case R.id.button_read_cache:
readFromCache("HelloWord.tex");//从files中读出数据
break;
case R.id.button_cache_dir:
cache();//在cache中写入、读出数据
break;
case R.id.button_sdcard:
sdcard();//在sdcard中写入、读出数据
break;
default:
break;
}
}

private void sdcard() {
File sdFile=new File(Environment.getExternalStorageDirectory().getAbsolutePath(),"hello.txt");
if(!sdFile.exists()){
try {
sdFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
//写入信息
try {
FileOutputStream outputStream=new FileOutputStream(sdFile);//已经初始化的文件用这种方式写入信息
PrintWriter printWriter=new PrintWriter(new OutputStreamWriter(outputStream));
printWriter.write(mEditText.getText().toString());//从edittext中写入信息
printWriter.flush();
printWriter.close();
outputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//读取信息
try {
FileInputStream inputStream=new FileInputStream(sdFile);//已经初始化的文件用这种方式读取信息
InputStreamReader isr=new InputStreamReader(inputStream);
BufferedReader br=new BufferedReader(isr);
String line=br.readLine();
while (line!=null){
Toast.makeText(getApplication(), "read_cache:" + line, Toast.LENGTH_SHORT).show();
line=br.readLine();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

private void cache() {
File file=new File(getCacheDir(),"HelloAndroid.txt");
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
//写入信息
try {
FileOutputStream outputStream=new FileOutputStream(file);//已经初始化的文件用这种方式写入信息
PrintWriter printWriter=new PrintWriter(new OutputStreamWriter(outputStream));
printWriter.write(mEditText.getText().toString());//从edittext中写入信息
printWriter.flush();
printWriter.close();
outputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//读取信息
try {
FileInputStream inputStream=new FileInputStream(file);//已经初始化的文件用这种方式读取信息
InputStreamReader isr=new InputStreamReader(inputStream);
BufferedReader br=new BufferedReader(isr);
String line=br.readLine();
while (line!=null){
Toast.makeText(getApplication(), "read_cache:" + line, Toast.LENGTH_SHORT).show();
line=br.readLine();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

private void readFromCache(String filename) {
try {
FileInputStream inputStream=openFileInput("filename");//未初始化的文件用这种方式读取信息
InputStreamReader isr=new InputStreamReader(inputStream);
BufferedReader br=new BufferedReader(isr);
String line=br.readLine();
while (line!=null){
Toast.makeText(getApplication(), "read_cache:"+line, Toast.LENGTH_SHORT).show();
line=br.readLine();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

private void writeToCache(String filename) {
try {
FileOutputStream outputStream=openFileOutput("filename", MODE_PRIVATE);//未初始化的文件用这种方式写入信息
PrintWriter printWriter=new PrintWriter(new OutputStreamWriter(outputStream));
printWriter.write(mEditText.getText().toString());//从edittext中写入信息
printWriter.flush();
printWriter.close();
outputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

private void ReadFromPreference() {
SharedPreferences preferences=getSharedPreferences("preferences_text", MODE_PRIVATE);
//        SharedPreferences preferences=getPreferences(MODE_PRIVATE);
String content=preferences.getString("edittext_input","我是默认值");
mTextView.setText(content);
}

private void WriteToPreference() {
SharedPreferences preferences=getSharedPreferences("preferences_text", MODE_PRIVATE);
//        SharedPreferences preferences=getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor=preferences.edit();
editor.putString("edittext_input",mEditText.getText().toString());
editor.commit();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: