2016-10-05 1 views
1

Wie erstelle ich gemeinsame Einstellungen für die interne Speicherdatei für meine Android App? Bitte geben Sie mir eine detaillierte Antwort.Wie erstelle ich gemeinsame Einstellungen für die interne Speicherdatei für meine Android App?

kind of like this

Ich will nur die Daten im internen Speicher oder die App speichern, indem Sie die Absenden-Button klicken und will auch die Daten später verwendet werden.

hier ist mein MainActivity.java

package com.example.ananaybatra.securityme; 

import android.net.Uri; 
import android.os.Bundle; 
import android.support.design.widget.FloatingActionButton; 
import android.support.design.widget.Snackbar; 
import android.util.Log; 
import android.view.View; 
import android.support.design.widget.NavigationView; 
import android.support.v4.view.GravityCompat; 
import android.support.v4.widget.DrawerLayout; 
import android.support.v7.app.ActionBarDrawerToggle; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.Button; 
import android.widget.EditText; 

import com.google.android.gms.appindexing.Action; 
import com.google.android.gms.appindexing.AppIndex; 
import com.google.android.gms.appindexing.Thing; 
import com.google.android.gms.common.api.GoogleApiClient; 

public class MainActivity extends AppCompatActivity 
     implements NavigationView.OnNavigationItemSelectedListener { 

    public static final String PREFS_NAME = "MyPrefsFile"; 



    /** 
    * ATTENTION: This was auto-generated to implement the App Indexing API. 
    * See https://g.co/AppIndexing/AndroidStudio for more information. 
    */ 
    private GoogleApiClient client; 

    Button mButton; 
    EditText mEdit; 

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




     mButton = (Button)findViewById(R.id.button); 
     mEdit = (EditText)findViewById(R.id.Name); 

     mButton.setOnClickListener(
       new View.OnClickListener() 
       { 
        public void onClick(View view) 
        { 
         Log.v("EditText", mEdit.getText().toString()); 
        } 
       }); 

     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
     fab.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
         .setAction("Action", null).show(); 
      } 
     }); 

     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
       this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
     drawer.setDrawerListener(toggle); 
     toggle.syncState(); 

     NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
     navigationView.setNavigationItemSelectedListener(this); 
     // ATTENTION: This was auto-generated to implement the App Indexing API. 
     // See https://g.co/AppIndexing/AndroidStudio for more information. 
     client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build(); 
    } 

    @Override 
    public void onBackPressed() { 
     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     if (drawer.isDrawerOpen(GravityCompat.START)) { 
      drawer.closeDrawer(GravityCompat.START); 
     } else { 
      super.onBackPressed(); 
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 

    @SuppressWarnings("StatementWithEmptyBody") 
    @Override 
    public boolean onNavigationItemSelected(MenuItem item) { 
     // Handle navigation view item clicks here. 
     int id = item.getItemId(); 

     if (id == R.id.nav_camera) { 
      // Handle the camera action 
     } else if (id == R.id.nav_gallery) { 

     } else if (id == R.id.nav_slideshow) { 

     } else if (id == R.id.nav_manage) { 

     } else if (id == R.id.nav_share) { 

     } else if (id == R.id.nav_send) { 

     } 

     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     drawer.closeDrawer(GravityCompat.START); 
     return true; 
    } 

    /** 
    * ATTENTION: This was auto-generated to implement the App Indexing API. 
    * See https://g.co/AppIndexing/AndroidStudio for more information. 
    */ 
    public Action getIndexApiAction() { 
     Thing object = new Thing.Builder() 
       .setName("Main Page") // TODO: Define a title for the content shown. 
       // TODO: Make sure this auto-generated URL is correct. 
       .setUrl(Uri.parse("http://[ENTER-YOUR-URL-HERE]")) 
       .build(); 
     return new Action.Builder(Action.TYPE_VIEW) 
       .setObject(object) 
       .setActionStatus(Action.STATUS_TYPE_COMPLETED) 
       .build(); 
    } 

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

     // ATTENTION: This was auto-generated to implement the App Indexing API. 
     // See https://g.co/AppIndexing/AndroidStudio for more information. 
     client.connect(); 
     AppIndex.AppIndexApi.start(client, getIndexApiAction()); 
    } 

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

     // ATTENTION: This was auto-generated to implement the App Indexing API. 
     // See https://g.co/AppIndexing/AndroidStudio for more information. 
     AppIndex.AppIndexApi.end(client, getIndexApiAction()); 
     client.disconnect(); 
    } 
} 

hier mein content_main.xml ist:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/content_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:showIn="@layout/app_bar_main" 
    tools:context="com.example.ananaybatra.securityme.MainActivity"> 

    <TextView 
     android:text="Enter Name" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_marginTop="18dp" 
     android:id="@+id/textView4" 
     android:textAppearance="@style/TextAppearance.AppCompat.Body2" /> 

    <EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:inputType="textPersonName" 
     android:ems="10" 
     android:layout_below="@+id/textView4" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:id="@+id/Name" /> 

    <TextView 
     android:text="Enter Phone Number" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/Name" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:id="@+id/textView5" 
     android:textAppearance="@style/TextAppearance.AppCompat.Body2" /> 

    <EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:inputType="phone" 
     android:ems="10" 
     android:layout_below="@+id/textView5" 
     android:layout_alignRight="@+id/Name" 
     android:layout_alignEnd="@+id/Name" 
     android:id="@+id/PhoneNumber" /> 

    <TextView 
     android:text="Enter Emergency Contact Number" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/PhoneNumber" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:id="@+id/textView6" 
     android:textAppearance="@style/TextAppearance.AppCompat.Body2" /> 

    <EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:inputType="phone" 
     android:ems="10" 
     android:layout_below="@+id/textView6" 
     android:layout_alignRight="@+id/textView6" 
     android:layout_alignEnd="@+id/textView6" 
     android:id="@+id/EmergencyContact" /> 

    <Button 
     android:text="SUBMIT" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/Submit" 
     android:layout_alignParentBottom="true" 
     android:layout_toRightOf="@+id/textView5" 
     android:layout_toEndOf="@+id/textView5" 
     android:layout_marginBottom="99dp" /> 

    <TextView 
     android:text="Enter Email ID" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/textView7" 
     android:textAppearance="@style/TextAppearance.AppCompat.Body2" 
     android:layout_below="@+id/EmergencyContact" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" /> 

    <EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:inputType="textEmailAddress" 
     android:ems="10" 
     android:layout_centerVertical="true" 
     android:layout_alignRight="@+id/EmergencyContact" 
     android:layout_alignEnd="@+id/EmergencyContact" 
     android:id="@+id/EmailID" /> 

    <TextView 
     android:text="App Coded by Ananay Batra." 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     android:id="@+id/textView2" /> 
</RelativeLayout> 
+0

möglich Duplikat http://stackoverflow.com/questions/3851560/how-to-use-sharedpreferences –

+0

‚Wie gemeinsame Vorlieben interne Speicherdatei erstellen‘ ??? Tut mir leid, ich habe keine Ahnung, wo du redest. – greenapps

Antwort

1

zu https://developer.android.com/guide/topics/data/data-storage.html#filesInternal für das, was Sie finden wirklich tun wollen, wie für ein Objekt in Interner Speicher Speichern .

JSONObject jsonObject = new JSONObject(); 

jsonObject.put("name", nameValue); 
jsonObject.put("phoneNumber", phoneNumberValue); 
jsonObject.put("emergencyPhoneNumber", emergencyPhoneNumberValue); 
jsonObject.put("email", emailValue); 

writeFile(context, String.format("%s/%s", context.getFilesDir(), "File name"), jsonObject.toString()); 

public static void writeFile(Context context, String filePath, String content) throws IOException { 

    FileWriter writer = new FileWriter(filePath, false); 
    writer.write(content); 

    writer.close(); 

} 
+0

Vielen Dank! Ich werde es mir ansehen. –

+0

Sie können diese Antwort markieren, wenn dies Ihre Frage beantwortet hat. – Bryan

Verwandte Themen