0

Я создал форму для связи с Android-приложением на панели действий, я добавил ее в свое приложение, и она открывается и выглядит отлично. Единственное, что я не могу найти, - это установить onClickListner для кнопки отправки, чтобы после нажатия кнопки отправки сообщение из формы должно было доставить непосредственно в мой почтовый ящик. Пожалуйста, помогите, вот мой код для этого.Как установить onClickListner для связи с нами в андроид-приложении

menu_main.xml

<item 
     android:id="@+id/contact_us" 
     android:title="Contact us" 
     android:showAsAction="ifRoom" 
     android:icon="@drawable/contact_us" 
     /> 

контакт для разметки

contact_us.xml

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="#ffffff" 
     android:orientation="vertical" 
     android:padding="16dp"> 

     <TextView 
      android:id="@+id/contact_form_title" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_horizontal" 
      android:layout_marginBottom="16dp" 
      android:layout_marginTop="5dp" 
      android:text="contact us" 
      android:textColor="#000000" 
      android:textSize="20sp" 
      android:typeface="serif" /> 

     <EditText 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:backgroundTint="@color/colorPrimaryDark" 
      android:hint="name" 
      android:inputType="textPersonName" /> 

     <EditText 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="10dp" 
      android:layout_marginTop="10dp" 
      android:backgroundTint="@color/colorPrimaryDark" 
      android:hint="Email" 
      android:inputType="textEmailAddress" /> 

    <EditText 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:backgroundTint="@color/colorPrimaryDark" 
     android:hint="phone number" 
     android:inputType="phone" /> 

    <EditText 
     android:layout_width="fill_parent" 
     android:layout_height="150dp" 
     android:layout_marginBottom="10dp" 
     android:layout_marginTop="10dp" 
     android:backgroundTint="@color/colorPrimaryDark" 
     android:gravity="top" 
     android:hint="your Message" 
     android:fitsSystemWindows="true" 
     android:breakStrategy="balanced" 
     android:inputType="textMultiLine" 
     android:singleLine="false" 
     android:padding="5dp" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" 
     android:background="@color/colorPrimaryDark" 
     android:elevation="4dp" 
     android:paddingLeft="70dp" 
     android:paddingRight="70dp" 
     android:text="Submit" 
     android:textColor="#fff" /> 
</LinearLayout> 

MainActivity.java

@Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
     return true; 
    } 

    public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
case R.id.contact_us: { 
       setContentView(R.layout.contact_us); 
       return true; 
      } 
      } 
      return super.onOptionsItemSelected(item); 

ответ

0

Вы можете попробовать добавить OnClick из XML как это:

<Button 
     android:id="@+id/button1" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:onClick="clickEvent" 
     android:text="Button 1" /> 
<Button 
     android:id="@+id/button2" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:onClick="clickEvent" 
     android:text="Button 2" /> 

Затем перейдите к основной деятельности:

public void clickEvent(View v) { 
     if (v.getId() == R.id.button1) { 
      Toast.makeText(MainActivity.this, "you click on button 1", 
        Toast.LENGTH_SHORT).show(); 
     } 

     if (v.getId() == R.id.button2) { 
      Toast.makeText(MainActivity.this, "you click on button 2", 
        Toast.LENGTH_SHORT).show(); 
     } 
    } 
0

Добавьте этот код в MainActivity на OnCreate метод:

final Button item = (Button) findViewById(R.id.btn); 
item.setOnClickListener(new OnClickListener() { 
    @Override 
    public void onClick() { 

    } 
}); 

И этот код атрибута Button в вашем XML-документа:

android:id="@+id/btn" 
0

Ass запустите идентификатор вашей кнопки, добавив android:id="@+id/submit_button к тегу Button в вашем XML-документе.

Затем в OnCreate метод вашей деятельности, добавьте следующий код

Button submitButton = (Button) findViewById(R.id.submit_button); 
submitButton.setOnClickListener(new OnClickListener() { 
    @Override 
    public void onClick() { 
     Intent intent = new Intent(Intent.ACTION_SEND); 
     intent.setType("message/rfc822"); 
     intent.putExtra(Intent.EXTRA_EMAIL , new String[]{"[email protected]"}); 
     intent.putExtra(Intent.EXTRA_SUBJECT, "subject of email"); 
     intent.putExtra(Intent.EXTRA_TEXT , "body of email"); 
     try { 
      startActivity(Intent.createChooser(intent, "Send mail")); 
     } catch (android.content.ActivityNotFoundException e) { 
      Toast.makeText(MyActivity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show(); 
     } 
    } 
}); 

Само собой разумеется, заменить идентификатор, предмет и тело, чтобы удовлетворить ваши потребности.

Надеется, что это помогает

+0

с этим кодом, приложение дает возможность всем клиентов по электронной почте по телефону, но то, что я хочу, а не внешних клиентов электронной почты, приложение должно иметь возможность отправлять электронную почту непосредственно из самого приложения. –

Смежные вопросы