EditText is also known as text field in HTML and some other languages, in android we use the term EditText
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">EditText Example</string>
<string name="button_name">Display Entered Text</string>
</resources>
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"
tools:context=".MainActivity" >
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="32dp"
android:ems="10"
android:inputType="text"
/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText1"
android:layout_centerHorizontal="true"
android:layout_marginTop="41dp"
android:text="@string/button_name" />
MainActivity.java
import
android.app.Activity;
import
android.os.Bundle;
import
android.view.View;
import
android.view.View.OnClickListener;
import
android.widget.Button;
import
android.widget.EditText;
import
android.widget.Toast;
public
class MainActivity extends Activity {
EditText edit;
Button button;
@Override
protected
void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit
= (EditText) findViewById(R.id.editText1);
button
= (Button) findViewById(R.id.button1);
button.setOnClickListener(new
OnClickListener() {
@Override
public
void onClick(View v) {
String
name = edit.getText().toString();
Toast.makeText(getApplicationContext(),
" " + name,
Toast.LENGTH_LONG).show();
}
});
}
}
No comments:
Post a Comment