【问题标题】:Storing User Input From a TextField存储来自 TextField 的用户输入
【发布时间】:2016-03-08 08:28:08
【问题描述】:

我正在创建的应用程序中有一个部分允许用户将信息输入到八个不同的文本字段中。这些字段是:

  1. 姓名
  2. 地址
  3. 城市
  4. 状态
  5. 邮编
  6. 电话
  7. 电子邮件
  8. 年龄

现在,当此信息完成并且用户点击提交按钮时,我希望在点击按钮后将信息直接发送到我的个人电子邮件地址,或者存储信息以供以后查看。我对 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();

    }
}

提前致谢!

【问题讨论】:

  • 我认为这个问题是可以理解的,所以我不明白为什么它被否决了。
  • 谢谢 Adam 我会看看的。
  • 不要忘记更改字符串名称 = regName.getText().toString(); to String name = userName.getText().toString();

标签: java android


【解决方案1】:

在您的情况下,您有 2 个发送电子邮件的选项:

  1. 使用应用程序发送电子邮件:在这种情况下,您唯一能做的就是启动电子邮件应用程序,并在电子邮件正文中预先填充您要发送的所有信息。但是,在这种情况下,您必须再次按最终的“发送”。您可以在这里查看详细信息 - How to open Email program via Intents (but only an Email program)

  2. 与 MailChimp、sendgrid 等任何电子邮件提供商集成,并直接从您的应用程序或通过您的后端进行 API 调用。

【讨论】:

  • 感谢 Puran 提供的信息。
【解决方案2】:

我已经用这个解决方案复制了你的问题,它没有经过测试,但我相信它会为你指明一个好的方向。

要保存在您的应用中,您可以这样做

布局文件

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

在这种情况下,它只会启动一个客户端电子邮件应用程序

  1. 如果您想使用按钮单击发送包含所有用户输入信息的电子邮件,请参考此链接 - Sending Email in Android using JavaMail API without using the default/built-in app

【讨论】:

    猜你喜欢
    • 2017-01-07
    • 1970-01-01
    • 2018-09-22
    • 2016-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多