Android ile Contact Form oluşturmaya çalıştım. Bu örnekte önemli olan
android:lines="4" android:singleLine="false"
ile textarea oluşturuyoruz.
android:singleLine="true"
bu da girilen değerin tek satırda yazılmasını sağlar.
Internette genelde email kontrolü için güzel bir tane RE kullanılmış. Düzenli ifadeler örneklerine bakmak için Regular Expression linkini tıklayın. How to Find or Validate an Email Address Java Example linkinde daha önce yazdığım email kontrolü yapan kod örneğine bakabilirisniz.
Daha önceki activityleri sonlandırmak için aşağıdaki kod kullanılır. Android bilmediğimden bugların neyden kaynaklandığını kestiremiyorum. Uygulama geliştiriken karşılaştığım bir bugı 3-4 gün çözemedim. Tesadüfen dün okuduğum bir makeleden çözümü buldum. Çözüm aşağıdaki tek satırlık kodmuş:)
intent_contact.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Android Option Menu and Submenu Sample bakmayı unutmayın.

option menu
Android contact form

form android
main.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:orientation="vertical">
<TextView android:text="First-last Name:*" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<EditText android:id="@+id/editText1_name" android:layout_height="wrap_content" android:layout_width="match_parent" android:text="" android:maxLength="50" android:singleLine="true"></EditText>
<TextView android:text="E-mail Address:*" android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<EditText android:id="@+id/editText2_mail" android:layout_height="wrap_content" android:layout_width="match_parent" android:text="" android:maxLength="50" android:singleLine="true"></EditText>
<TextView android:text="Message:*" android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<EditText android:id="@+id/editText3_message" android:layout_height="wrap_content" android:layout_width="match_parent" android:text="" android:maxLength="250" android:lines="4" android:singleLine="false"></EditText>
<Button android:text="Send" android:id="@+id/button1_send" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
MyStartActivity.java
package com.form.contact;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
public class MyStartActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater=getMenuInflater();
menuInflater.inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.item1_contact:
Intent intent_contact=new Intent(getApplicationContext(), ContactFormAnd.class);
//Daha önceki activityler sonlandırılır ve bu başlatılır.
intent_contact.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent_contact);
break;
}
return super.onOptionsItemSelected(item);
}
}
ContactFormAnd.java
package com.form.contact;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class ContactFormAnd extends Activity{
private EditText editText_name;
private EditText editText_mail;
private EditText editText_message;
private Button button_send;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
editText_name=(EditText) findViewById(R.id.editText1_name);
editText_mail=(EditText) findViewById(R.id.editText2_mail);
editText_message=(EditText) findViewById(R.id.editText3_message);
button_send=(Button) findViewById(R.id.button1_send);
}
@Override
protected void onResume() {
super.onResume();
button_send.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
String name=editText_name.getText().toString();
String mail=editText_mail.getText().toString();
String message=editText_message.getText().toString();
if(name.length()>3 && mail.length()>3 && message.length()>6)
{
if(ContactConfig.checkEmail(mail))
{
//Tabi toast mesajı göstermek yerine girilen bilgileri now() sql functionu ile şuanki tarih te eklenerek database kayedilir genelde.
Toast.makeText(getApplicationContext(), "Send your message", Toast.LENGTH_SHORT).show();
editText_name.setText("");
editText_mail.setText("");
editText_message.setText("");
}else{
Toast.makeText(getApplicationContext(), "Invalid e-mail address", Toast.LENGTH_SHORT).show();
}
}else
{
Toast.makeText(getApplicationContext(), "Invalid field data", Toast.LENGTH_SHORT).show();
}
}
});
}
}
ContactConfig.java
package com.form.contact;
import java.util.regex.Pattern;
public class ContactConfig {
public final static Pattern EMAIL_ADDRESS_PATTERN =
Pattern.compile(
"[a-zA-Z0-9\\+\\.\\_\\%\\-\\+]{1,256}" +
"\\@" +
"[a-zA-Z0-9][a-zA-Z0-9\\-]{0,64}" +
"(" +
"\\." +
"[a-zA-Z0-9][a-zA-Z0-9\\-]{0,25}" +
")+"
);
public static boolean checkEmail(String email) {
return EMAIL_ADDRESS_PATTERN.matcher(email).matches();
}
}
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, ContactFormAnd!</string>
<string name="app_name">Contact Form</string>
<string name="contact">Contact Form</string>
</resources>
menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:showAsAction="ifRoom" android:id="@+id/item1_contact" android:title="@string/contact"></item>
</menu>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.form.contact"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MyStartActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="ContactFormAnd"></activity>
</application>
</manifest>
Download ContactFormSample.tar.gz