简体   繁体   English

从文本字段存储用户输入

[英]Storing User Input From a TextField

There's a section within the app I'm creating that allows users to input information into eight different TextFields. 我正在创建的应用程序中有一个部分,允许用户将信息输入到八个不同的TextField中。 Those fields are: 这些字段是:

  1. Name 名称
  2. Address 地址
  3. City
  4. State
  5. Zip 压缩
  6. Phone 电话
  7. Email 电子邮件
  8. Age 年龄

Now when this information is completed and the user taps the submit button I'm either wanting the information to be sent to my personal email address directly after hitting the button or storing the information to be viewed for a later time. 现在,当这些信息完成并且用户点击了“提交”按钮后,我要么希望将信息点击按钮后直接发送到我的个人电子邮件地址,要么希望将信息存储起来以便以后查看。 I'm relatively new to Android development, however, I was thinking that I should do the following within a method that I would call when the button is tap like so: 我是Android开发的新手,但是我想我应该在按下按钮时调用的方法中执行以下操作:

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

I've searched and searched and can't find anything that I'm trying to accomplish. 我已经搜索了很多东西,却找不到我想要完成的任何事情。 Is there possibly a way to send the information directly to my email? 是否可以将信息直接发送到我的电子邮件中? or do I need to store it somehow for later viewing. 还是我需要以某种方式存储它以便以后查看。 Heres my Java file I have so far: 到目前为止,这是我的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();

    }
}

Thanks in advance! 提前致谢!

In your case you have 2 options to send email: 在您的情况下,您有2种发送电子邮件的选项:

  1. Send email using the app: In this case only thing you can do is launch email app with all the information you want to send pre-populated in the email body. 使用该应用发送电子邮件:在这种情况下,您唯一可以做的就是启动电子邮件应用,并在电子邮件正文中预先填充要发送的所有信息。 But, in this case you have to press final "Send" again. 但是,在这种情况下,您必须再次按下最终的“发送”。 You can see the details here - How to open Email program via Intents (but only an Email program) 您可以在此处查看详细信息- 如何通过Intents打开电子邮件程序(但仅限电子邮件程序)

  2. Integrate with any email providers like MailChimp, sendgrid etc and make an API call directly from your app or through your backend. 与任何电子邮件提供商(例如MailChimp,sendgrid等)集成,并直接从您的应用程序或通过后端进行API调用。

I have replicated your problem with this solution, It is not tested but I believe it will point you to a good direction. 我已经使用此解决方案复制了您的问题,虽然尚未测试,但我相信它将为您指明一个正确的方向。

To save within your app, you might do this 要保存在您的应用程序中,您可以执行此操作

Layout file 布局文件

<?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>

Activity Page 活动页面

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;
}
}

For sending the information to your email, you can either use the 要将信息发送到您的电子邮件,您可以使用

  1. Android Intent Android意图

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

In this case, it will just launch a client email application 在这种情况下,它将仅启动客户端电子邮件应用程序

  1. If you want to use a button click to send email with all user inputted information available, then refer to this link - Sending Email in Android using JavaMail API without using the default/built-in app 如果要使用按钮单击来发送包含所有用户输入信息的电子邮件,请参考此链接- 使用JavaMail API在Android中发送电子邮件,而无需使用默认/内置应用程序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM