Below is example of EditText, it can be used to enter text. Example demonstrates usage of other attributes and methods.
I used Eclipse as development tool and tested the sample in Emulator for Android 4.0.3.
There are resources are used to develop android app like image files etc. but for this example, we are using three basis components like code base file, layout file and string file.
Source of Layout File(.XML):
<LinearLayout 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:orientation="vertical" >
<EditText
android:id="@+id/etText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/ettext"
/>
<Button
android:id="@+id/btnselectall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btnselectall"
/>
<Button
android:id="@+id/btnsetselection"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btnsetselection"
/>
<Button
android:id="@+id/btnsettext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btnsettext"
/>
<Button
android:id="@+id/btnclose"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btnclose"
/>
</LinearLayout >
Source of Strings file(.XML):
<resources>
<string name="app_name">EditTextExample</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_edit_text_example">Edit Text Example</string>
<string name="ettext">Enter Text</string>
<string name="btnselectall">Select All</string>
<string name="btnsetselection">Set Selection</string>
<string name="btnsettext">Set Text</string>
<string name="btnclose">Close</string>
</resources>
Source code file (.JAVA):
package com.example.edittextexample;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class EditTextExample extends Activity {
private Button btnSelectAll, btnClose, btnSetSelection, btnSetText;
private EditText etText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_text_example);
btnSelectAll = (Button) findViewById(R.id.btnselectall);
etText = (EditText) findViewById(R.id.etText);
btnClose = (Button) findViewById(R.id.btnclose);
btnSetSelection = (Button) findViewById(R.id.btnsetselection);
btnSetText = (Button) findViewById(R.id.btnsettext);
etText.setSingleLine(); //allows to be text in single line if it is not there then longer text will be in next lines. you can use "setHorizontallyScrolling" method as well
addOnOkClickListener();
addOnCloseListener();
addOnSetSelectionListener();
addOnClickSetTextListener();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_edit_text_example, menu);
return true;
}
public void addOnOkClickListener (){
btnSelectAll.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
etText.selectAll(); //select all text of EditText
}
});
}
public void addOnCloseListener(){
btnClose.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
finish();
}
});
}
public void addOnSetSelectionListener(){
btnSetSelection.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
etText.setSelection(0,4); //method is used to select specific character from EditText, start and end range is required, start = 0 means first character. and "End" parameter allows how many characters to be selected.
}
});
}
public void addOnClickSetTextListener(){
btnSetText.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
etText.setText("Hello World!!!"); //setText method is used to set Text to EditText
}
});
}
}
Note:
- "Select All" selects text entered in text field
- "Set Selection" selects first 4 characters of entered text
- "Set Text" sets value like "Hello World!!!" to edit text
- "Close" will close the application
No comments:
Post a Comment