2016-03-07 79 views
-1

我創建的應用程序中有一部分允許用戶將信息輸入到八個不同的TextField中。這些領域是:從TextField存儲用戶輸入

  1. 名稱
  2. 地址
  3. 國家
  4. 郵編
  5. 電話
  6. 電子郵件
  7. 年齡

現在,當這些信息完成並且用戶點擊提交按鈕時,我希望在點擊按鈕之後直接將信息發送到我的個人電子郵件地址,或者稍後存儲要查看的信息。我是比較新的Android開發,但是,我想,我應該做的方法中的以下,我會打電話的時候,按鈕是挖掘像這樣:

final EditText moverName = (EditText)findViewById(R.id.nameRegistration); 
String name = regName.getText().toString(); 

我已經搜查,搜查,並能找不到我想要完成的任何事情。有沒有可能將信息直接發送到我的電子郵件?或者我需要以某種方式存儲它以備以後查看。我的Java文件我到目前爲止:

public class UserRegistrationActivity extends AppCompatActivity { 

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

    public void sendFeedback(View button) { 

     final EditText userName = (EditText)findViewById(R.id.nameRegistration); 
     String name = regName.getText().toString(); 

     final EditText userStreetAddress = (EditText)findViewById(R.id.userStreetAddressRegistration); 
     String address = userStreetAddress.getText().toString(); 

     final EditText userCity = (EditText)findViewById(R.id.userCityRegistration); 
     String city = userCity.getText().toString(); 

     final EditText userState = (EditText)findViewById(R.id.userStateRegistration); 
     String state = userState.getText().toString(); 

     final EditText userZip = (EditText)findViewById(R.id.userZipRegistration); 
     String zip = userZip.getText().toString(); 

     final EditText userPhone = (EditText)findViewById(R.id.userPhoneRegistration); 
     String phone = userPhone.getText().toString(); 

     final EditText userEmail = (EditText)findViewById(R.id.userEmailRegistration); 
     String email = userEmail.getText().toString(); 

     final EditText userDOB = (EditText)findViewById(R.id.userDOB); 
     String dob = userDOB.getText().toString(); 

    } 
} 

在此先感謝!

+1

你見過嗎? http://stackoverflow.com/questions/2020088/sending-email-in-android-using-javamail-api-without-using-the-default-built-in-a –

+0

我認爲這個問題是可以理解的,所以我不要」不明白爲什麼它被拒絕投票。 – mur7ay

+0

感謝Adam Ill看看它。 – mur7ay

回答

1

我已經複製了您的問題與此解決方案,它沒有測試,但我相信它會指出你一個很好的方向。

要在應用程序中保存,你可以這樣做

版式文件

<?xml version="1.0" encoding="utf-8"?> 
<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" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context="com.inducesmile.emailapplication.MainActivity"> 

<EditText 
    android:id="@+id/nameRegistration" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:hint="Enter name" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true"/> 

<EditText 
    android:id="@+id/userStreetAddressRegistration" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:hint="Enter Address" 
    android:layout_below="@+id/nameRegistration" 
    android:layout_marginTop="8dp" 
    android:layout_centerHorizontal="true"/> 
<EditText 
    android:id="@+id/userCityRegistration" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:hint="Enter City" 
    android:layout_below="@+id/userStreetAddressRegistration" 
    android:layout_marginTop="8dp" 
    android:layout_centerHorizontal="true"/> 

<EditText 
    android:id="@+id/userStateRegistration" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:hint="Enter State" 
    android:layout_below="@+id/userCityRegistration" 
    android:layout_marginTop="8dp" 
    android:layout_centerHorizontal="true"/> 


<EditText 
    android:id="@+id/userZipRegistration" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:hint="Enter Zip Code" 
    android:layout_below="@+id/userStateRegistration" 
    android:layout_marginTop="8dp" 
    android:layout_centerHorizontal="true"/> 

<EditText 
    android:id="@+id/userPhoneRegistration" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:hint="Enter Phone Number" 
    android:layout_below="@+id/userZipRegistration" 
    android:layout_marginTop="8dp" 
    android:layout_centerHorizontal="true"/> 

<EditText 
    android:id="@+id/userEmailRegistration" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:hint="Enter Email" 
    android:layout_below="@+id/userPhoneRegistration" 
    android:layout_marginTop="8dp" 
    android:layout_centerHorizontal="true"/> 

<EditText 
    android:id="@+id/userDOB" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:hint="Enter DOB" 
    android:layout_below="@+id/userEmailRegistration" 
    android:layout_marginTop="8dp" 
    android:layout_centerHorizontal="true"/> 

<Button 
    android:id="@+id/submit_button" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Submit" 
    android:padding="20dp" 
    android:layout_marginTop="16dp" 
    android:layout_below="@+id/userDOB" 
    android:layout_centerHorizontal="true"/> 

</RelativeLayout> 

活動頁面

import android.content.Context; 
import android.content.SharedPreferences; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 

import java.util.HashSet; 
import java.util.Set; 

public class MainActivity extends AppCompatActivity { 

private EditText userName; 
private EditText userStreetAddress; 
private EditText userCity; 
private EditText userState; 
private EditText userZip; 
private EditText userPhone; 
private EditText userEmail; 
private EditText userDOB; 

private SharedPreferences prefs; 

private boolean hasDataBeenSaved; 

private Set<String> set; 

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

    prefs=this.getSharedPreferences("settings", Context.MODE_PRIVATE); 
    set = prefs.getStringSet("Personal Information", null); 
    if(set.size() > 0){ 
     hasDataBeenSaved = true; 
    } 

    if(hasDataBeenSaved){ 
     // display the store personal information 

    } 

    userName = (EditText)findViewById(R.id.nameRegistration); 
    userStreetAddress = (EditText)findViewById(R.id.userStreetAddressRegistration); 
    userCity = (EditText)findViewById(R.id.userCityRegistration); 
    userState = (EditText)findViewById(R.id.userStateRegistration); 
    userZip = (EditText)findViewById(R.id.userZipRegistration); 
    userPhone = (EditText)findViewById(R.id.userPhoneRegistration); 
    userEmail = (EditText)findViewById(R.id.userEmailRegistration); 
    userDOB = (EditText)findViewById(R.id.userDOB); 


    Button submitButton = (Button)findViewById(R.id.submit_button); 
    submitButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      if(!hasDataBeenSaved){ 
       // Data has not been save then save them 

       String name = userName.getText().toString(); 
       String address = userStreetAddress.getText().toString(); 
       String city = userCity.getText().toString(); 
       String state = userState.getText().toString(); 
       String zip = userZip.getText().toString(); 
       String phone = userPhone.getText().toString(); 
       String email = userEmail.getText().toString(); 
       String dob = userDOB.getText().toString(); 

       if(!isEmpty(name) || !isEmpty(address) || !isEmpty(city) || !isEmpty(state) || !isEmpty(zip) || !isEmpty(phone) || !isEmpty(email) || !isEmpty(dob)){ 
        Toast.makeText(MainActivity.this, "All input field must be filled", Toast.LENGTH_LONG).show(); 
        return; 
       } 

       Set<String> set = new HashSet<String>(); 
       set.add(name); 
       set.add(address); 
       set.add(city); 
       set.add(state); 
       set.add(zip); 
       set.add(phone); 
       set.add(email); 
       set.add(dob); 

       SharedPreferences.Editor edit = prefs.edit(); 
       edit.putStringSet("Personal Information", set); 
       edit.commit(); 
      } 
     } 
    }); 
} 

private boolean isEmpty(String input){ 
    if(input.equals("")){ 
     return true; 
    } 
    return false; 
} 
} 

對於將信息發送給您的電子郵件,您可以使用

  1. Android的意圖

    Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); 
    

在這種情況下,它只會推出一個客戶端電子郵件應用程序

  • 如果你想使用一個按鈕,點擊發送電子郵件所有可用的用戶輸入的信息,然後參考此鏈接 - Sending Email in Android using JavaMail API without using the default/built-in app
  • 2

    在你的情況,你有2個選擇發送電子郵件:使用的應用程序

    1. 發送電子郵件:在這種情況下,唯一可以做的事情是所有要發送前的信息發佈電子郵件應用程序在電子郵件正文中填充。但是,在這種情況下,您必須再次按最後的「發送」。你可以在這裏看到的細節 - How to open Email program via Intents (but only an Email program)

    2. 與任何電子郵件提供商,如MailChimp,sendgrid等集成,並直接從您的應用程序或通過您的後端進行API調用。

    +0

    感謝您的信息普蘭。 – mur7ay