Методические указания по выполнению лабораторных работ по дисциплине «программирование мобильных устройств»


if (addToBackStack) replaceTransaction  .addToBackStack(key); replaceTransaction  .commit();  }  public boolean



Pdf көрінісі
бет28/30
Дата20.10.2023
өлшемі3,51 Mb.
#186972
түріМетодические указания
1   ...   22   23   24   25   26   27   28   29   30
Байланысты:
15.-Metod LR Programmirovanie-mobil nyh-ustroistv 09.03.02 2021

if
(addToBackStack) replaceTransaction 
.addToBackStack(key); replaceTransaction 
.commit(); 

public boolean 
returnToBackStack(String stackKey,
 boolean 
inclusive) { 
return
getSupportFragmentManager() 
.popBackStackImmediate(stackKey,
inclusive

FragmentManager.
POP_BACK_STACK_INCLUSIVE
: 0); 

public
Bus getBus() { 
return bus



75 


Листинг BaseFragment.java 
package
 
ru.omgtu.myapplication;
 
import
 
android.os.Bundle; 
import
android.support.annotation.Nullable;
import 
android.support.v4.app.Fragment; 
import
android.view.View; 
import
com.squareup.otto.Bus; 
import
butterknife.ButterKnife; 
public class 
BaseFragment
 extends 
Fragment { 
private
Bus
 bus
; @Override 
public void 
onCreate(@Nullable Bundle savedInstanceState) { 
super
.onCreate(savedInstanceState); 

@Override 
public void 
onStart() { 
super
.onStart(); 
bus
= ((BaseActivity) getActivity()).getBus(); 
bus
.register(
this
); 

@Override 
public void 
onStop() {
 super
.onStop();
 bus
.unregister(
this
); 

@Override 
public void 
onDestroy() { 
super
.onDestroy(); 

@Override 
public void 
onViewCreated(View view, @Nullable Bundle savedInstanceState) 
{
 super
.onViewCreated(view, savedInstanceState); ButterKnife.
bind
(
this
, view); 

76 


@Override 
public void 
onDestroyView() {
 super
.onDestroyView(); ButterKnife.
unbind
(
this
); 

public
BaseActivity getBaseActivity() { 
return
(BaseActivity) getActivity(); 

public
Bus getBus() { 
return bus



Листинг
MainActivity.java
package
ru.omgtu.myapplication;
import 
android.content.Intent; 
import
android.os.Bundle; 
import
com.squareup.otto.Subscribe; 
public class 
MainActivity
 extends 
BaseActivity { @Override 
protected void 
onStop() { 
super
.onStop(); 

@Override 
protected
void
onCreate(Bundle
savedInstanceState)

super
.onCreate(savedInstanceState); setContentView(R.layout.
activity_main
); 
if
(savedInstanceState ==
 null

getSupportFragmentManager().beginTransaction().add(R.id.
container

new
HW7MenuFragment()).commit();
 

@Subscribe 
public void 
onOpenSaveInSharedPreferencesFragmentEvent(OpenSaveInSharedP
 
referencesFragmentEvent event) { 

78 


replaceFragment(
new
SaveInSharedPreferencesFragment(), 
true
); 
@Subscribe 
public void
onOpenSaveInFileFragmentEvent(OpenSaveInFileFragmentEvent 
event) { 
replaceFragment(
new
SaveInFileFragment(), 
true
); 


Листинг HW7MenuFragment.java 
package
 
ru.omgtu.myapplication;
 
import
 
android.os.Bundle; 
import
android.support.v4.app.Fragment;
 import 
android.view.LayoutInflater;
 
import 
android.view.View; 
import
android.view.ViewGroup;
 import 
butterknife.ButterKnife;
 import 
butterknife.OnClick; 
public class 
HW7MenuFragment
 extends 
BaseFragment 
{
 public
HW7MenuFragment() { 
// Required empty public constructor 

@Override 
public
View onCreateView(LayoutInflater inflater, ViewGroup 
container,
 
Bundle savedInstanceState) { 
View view = inflater.inflate(R.layout.
fragment_hw7_menu
, container, 
false
); ButterKnife.
bind
(
this
, view); 
return
view; 

@Override 
public void 
onDestroyView() {
 super
.onDestroyView(); ButterKnife.
unbind
(
this
); 

78 


@OnClick(R.id.
save_in_shared_preferences_button

public void 
onSaveInSharedPreferencesButtonClick() { getBus().post(
new 
OpenSaveInSharedPreferencesFragmentEvent()); 

@OnClick(R.id.
save_in_file_button

public
void
onSaveInFileButtonClick()
{
getBus().post(
new 
OpenSaveInFileFragmentEvent()); 

@OnClick(R.id.
phone_book_button

public
void
onPhoneBookButtonClick()
{
getBus().post(
new 
OpenPhoneBookFragmentEvent()); 


Листинг SaveInSharedPreferencesFragment.java 
package
ru.omgtu.myapplication; 
import
android.content.Context; 
import
android.content.SharedPreferences; 
import
android.os.Bundle; 
import
android.support.v4.app.Fragment;
 
import 
android.view.LayoutInflater;
 
import 
android.view.View; 
import
android.view.ViewGroup;
import
android.widget.EditText;
import 
butterknife.Bind; 
import
butterknife.ButterKnife; 
public class 
SaveInSharedPreferencesFragment
 
extends 
BaseFragment { 
public static final 
String
 
APP_PREFERENCES
 
=
"myPreferences"
;
public 
static final 
String
 
APP_PREFERENCES_TEXT
 
=
"Text"
; SharedPreferences
 
sharedPreferences
; @Bind(R.id.
edit_text_for_save

EditText 
editTextForSave

83 


public
SaveInSharedPreferencesFragment() 
{
 
// Required empty public constructor 

@Override 
public
View onCreateView(LayoutInflater inflater, ViewGroup 
container,
 
Bundle savedInstanceState) { 
View view = inflater.inflate(R.layout.
fragment_save_text
, container, 
false
); ButterKnife.
bind
(
this
, view); 
sharedPreferences
= getContext().getSharedPreferences(
APP_PREFERENCES
,
 
Context.
MODE_PRIVATE
); 
return
view; 

@Override 
public void 
onDestroyView() {
 super
.onDestroyView(); ButterKnife.
unbind
(
this
); 

@Override 
public void 
onPause() { 
super
.onPause(); 
SharedPreferences.Editor
editor
=
sharedPreferences
.edit(); 
editor.putString(
APP_PREFERENCES_TEXT

editTextForSave
.getText().toString()); editor.apply(); 

@Override 
public void 
onResume() { 
super
.onResume(); 
if
(
sharedPreferences
.contains(
APP_PREFERENCES_TEXT
))

editTextForSave
.setText(
sharedPreferences
.getString(
APP_PREFER ENCES_TEXT
,
 
""
)); 


84 



Листинг
SaveInFileFragment.java
package
ru.omgtu.myapplication;
import 
android.content.Context; 
import
android.os.Bundle; 
import
android.os.Environment; 
import
android.support.v4.app.Fragment; 
import
android.util.Log; 
import
android.view.LayoutInflater; 
import
android.view.View;
import
android.view.ViewGroup;
import 
android.widget.EditText; 
import
android.widget.Toast; 
import
ru.omgtu.myapplication.R; 
import
java.io.BufferedReader; 
import
java.io.File; 
import
java.io.FileInputStream;
 import 
java.io.FileNotFoundException;
 import 
java.io.FileOutputStream; 
import
java.io.InputStream; 
import
java.io.InputStreamReader;
import
java.io.OutputStream;
import 
java.io.OutputStreamWriter; 
import
butterknife.Bind; 
import
butterknife.ButterKnife; 
public class 
SaveInFileFragment
 extends 
BaseFragment {
 public static final 
String 
FILE_NAME

"fileForSaveText.txt"
; File 
file

@Bind(R.id.
edit_text_for_save
) EditText 
editTextForSave

public
SaveInFileFragment() { 
// Required empty public constructor 

@Override 
public
View onCreateView(LayoutInflater inflater, ViewGroup 
container,
 
Bundle savedInstanceState) { 
// Inflate the layout for this fragment 
View view = inflater.inflate(R.layout.
fragment_save_text
, container, 
false
); ButterKnife.
bind
(
this
, view); 
return
view; 

@Override 
85 


public void 
onDestroyView() {
 super
.onDestroyView(); ButterKnife.
unbind
(
this
); 

@Override 
public void 
onPause() { 
super
.onPause();
 try 

OutputStream
outputStream
=
getContext().openFileOutput(
FILE_NAME

Context.
MODE_PRIVATE
); 
OutputStreamWriter osw = 
new 
OutputStreamWriter(outputStream); 
osw.write(
editTextForSave
.getText().toString()); osw.close(); 
outputStream.close(); 

catch
(Throwable t) { Toast.
makeText
(getContext(), 
"Exception: "
+ t.toString(), 
Toast.
LENGTH_LONG
).show();
 


@Override 
public void 
onResume() { 
super
.onResume();
 try 

InputStream inputStream = getContext().openFileInput(
FILE_NAME
); 
InputStreamReader isr = 
new
InputStreamReader(inputStream); BufferedReader 
reader = 
new
BufferedReader(isr); 
editTextForSave
.setText(reader.readLine()); 
reader.close(); isr.close(); inputStream.close(); 

catch
(Throwable t) { Toast.
makeText
(getContext(), 
"Exception: "
+ t.toString(), 
Toast.
LENGTH_LONG
).show();
 



86 




Достарыңызбен бөлісу:
1   ...   22   23   24   25   26   27   28   29   30




©engime.org 2024
әкімшілігінің қараңыз

    Басты бет