通过电子邮件在android中发送联系表单

时间:2013-12-04 23:01:57

标签: android eclipse android-layout android-intent

我正在尝试创建一个基本的联系表单应用程序,人们可以在其中填写所需信息并按下发送按钮并将信息发送到特定的电子邮件。我把它放在一起但由于某种原因它不起作用。 Java文件中包含来自类似应用程序的代码。 Java文件中有spinner代码,所以我把它变成了笔记..不确定它是不是搞乱了。这是代码,请让我知道我可以做些什么来使它工作。

activity_main.xml中:

    <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=".MainActivity" >

        <EditText
            android:id="@+id/etName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/textView1"
            android:layout_below="@+id/textView1"
            android:ems="10"
            android:hint="Enter First and Last Name"
            android:inputType="textPersonName" >

            <requestFocus />
        </EditText>

        <EditText
            android:id="@+id/etPhone"      
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/etName"
            android:layout_marginTop="14dp"
            android:ems="10"
            android:hint="Enter Phone #"
            android:inputType="phone" />

        <EditText
            android:id="@+id/etEmail"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/etPhone"
            android:layout_marginTop="17dp"
            android:ems="10"
            android:hint="Enter E-mail"
            android:inputType="textEmailAddress" />

        <EditText
            android:id="@+id/etAdd"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"       
            android:layout_below="@+id/etEmail"
            android:layout_marginTop="18dp"
            android:ems="10"
            android:hint="Enter Additional Information Here"
            android:lines="3"
            android:inputType="textMultiLine" />

        <Button
            android:id="@+id/send"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_below="@+id/etAdd"
            android:text="Send" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/etPhone"
            android:layout_alignParentTop="true"
            android:gravity="center"
            android:text="Contact Form"
            android:textAppearance="?android:attr/textAppearanceMedium" />

    </RelativeLayout>

这是MainActivity.java文件:

package com.example.contactform1;

    import android.app.Activity;
    import android.app.AlertDialog;
    import android.content.DialogInterface;
    import android.content.Intent;
    import android.os.Bundle;
    import android.text.Html;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemSelectedListener;
    import android.widget.ArrayAdapter;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Spinner;
    import android.widget.Toast;

    public class MainActivity  extends Activity implements OnClickListener, OnItemSelectedListener{
    /** Called when the activity is first created. */

        EditText etname, etphone, etemail, etadd;

        //Spinner subject;

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

        etname = (EditText) findViewById (R.id.etName);

        etphone = (EditText) findViewById(R.id.etPhone);
        etemail = (EditText) findViewById(R.id.etEmail);       
        etadd = (EditText)findViewById(R.id.etAdd);

        //subject=(Spinner)findViewById(R.id.spinner);
        //String subjects[]=new String[]{"Default","Klacht","Vraag","Opmerking","Applicatie"};
        //subject.setOnItemSelectedListener(this);
        //ArrayAdapter<String> sa = new ArrayAdapter<String>(getApplicationContext(),
            //android.R.layout.simple_spinner_item, subjects);
        //sa.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
        //subject.setAdapter(sa);;        


        final Button buttonSend= (Button)findViewById(R.id.send);        
        buttonSend.setOnClickListener(this);
      }

    public void onClick(View v) 
    {
             //if(etname.getText().toString().length()==0)  
             //{           
              //etname.setError( "Vul uw naam in" );  
             //}  
             //else if(etphone.getText().toString().length()==0)  
             //{           
              //etphone.setError( "Vul uw email in" ); 
             //}
             //else if(etemail.getText().toString().length() != 10)  
             //{           
              //etemail.setError( "Vul een geldig telefoonnummer in" );
             //}
             //else if(etadd.getText().toString().length()==0)  
             //{           
              //etadd.setError( "Vul uw bericht in" );  
             //}
             //else if(subject.getSelectedItemPosition()==0)  
             //{           
              //Toast.makeText(MainActivity.this,"Please select the Subject",Toast.LENGTH_SHORT).show(); 
             //}
             //else
             //{  
                //String body=
             //"Name : "+etname.getText().toString()+"<br>Mobile :"+etphone.getText().toString()+
              //"<br>Email :"+etemail.getText().toString();//+"<br>Bericht :"+etadd.getText().toString();  

                //Intent email = new Intent(Intent.ACTION_SEND); 
                //email.putExtra(Intent.EXTRA_EMAIL, new String[]{"email here"});           
                //email.putExtra(Intent.EXTRA_SUBJECT, subject.getSelectedItem().toString()); 
                //email.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(body)); 
                //email.setType("message/rfc822");
                //startActivityForResult(Intent.createChooser(email, "marketing"),1); 
             }         
     // }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    {

        new AlertDialog.Builder(MainActivity.this)
    .setMessage("Your requested has been Accepted\nThank You")
    .setCancelable(false)
    .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) 
    {
      dialog.cancel();
        }
    })  
        .show();
    }

    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub

    }
    }

LogCat信息:

12-04 18:19:15.574: W/IInputConnectionWrapper(28530): showStatusIcon on inactive InputConnection
12-04 18:19:21.190: D/AbsListView(28530): Get MotionRecognitionManager
12-04 18:19:21.220: D/AbsListView(28530): onVisibilityChanged() is called, visibility : 4
12-04 18:19:21.220: D/AbsListView(28530): unregisterIRListener() is called 
12-04 18:19:21.230: D/AbsListView(28530): onVisibilityChanged() is called, visibility : 0
12-04 18:19:21.230: D/AbsListView(28530): unregisterIRListener() is called 
12-04 18:19:21.240: D/AbsListView(28530): unregisterIRListener() is called 
12-04 18:19:21.320: D/AbsListView(28530): unregisterIRListener() is called 
12-04 18:19:21.330: D/AbsListView(28530): unregisterIRListener() is called 
12-04 18:19:21.360: D/AbsListView(28530): unregisterIRListener() is called 
12-04 18:19:21.390: D/AbsListView(28530): unregisterIRListener() is called 
12-04 18:19:22.591: D/AbsListView(28530): unregisterIRListener() is called 
12-04 18:19:22.861: W/IInputConnectionWrapper(28530): showStatusIcon on inactive InputConnection
12-04 18:19:22.982: D/AbsListView(28530): onDetachedFromWindow
12-04 18:19:22.982: D/AbsListView(28530): unregisterIRListener() is called 
12-04 18:19:37.546: E/ViewRootImpl(28530): sendUserActionEvent() mView == null
12-04 18:19:40.499: W/IInputConnectionWrapper(28530): showStatusIcon on inactive InputConnection
12-04 18:19:40.519: E/OpenGLRenderer(28530): SFEffectCache:clear(), mSize = 0

我现在正在显示该表单,但您输入的信息未发送到该电子邮件。我被提示屏幕指示我应该使用哪个电子邮件服务来执行此应用程序。我只是希望输入的信息直接发送到应用程序中提供的电子邮件。

提前致谢!

1 个答案:

答案 0 :(得分:0)

您正在看到包含电子邮件客户端列表的屏幕,因为这是Intents的工作方式。

意图不会直接执行您想要的操作,它只能将数据移交给另一个可以处理数据的Activity(可能在不同的应用程序中)。