How to get text from EditText in Android

activity_main.xml
 <RelativeLayout     xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:id="@+id/rl"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:padding="10dp"     tools:context=".MainActivity"     android:background="#c6cabd"     >     <TextView         android:id="@+id/tv"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:textSize="17dp"         android:textColor="#ff0e13"         />     <EditText         android:id="@+id/et"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_below="@id/tv"         android:hint="Input your country"         />     <Button         android:id="@+id/btn"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="Get EditText Text"         android:layout_below="@id/et"         /> </RelativeLayout> 
MainActivity.java
 package com.cfsuman.me.androidcodesnippets;  import android.os.Bundle; import android.app.Activity; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView;   public class MainActivity extends Activity {      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);          // Get the widgets reference from XML layout         Button btn = (Button) findViewById(R.id.btn);         akibat TextView tv = (TextView) findViewById(R.id.tv);         akibat EditText et = (EditText) findViewById(R.id.et);          btn.setOnClickListener(new View.OnClickListener() {             @Override             public void onClick(View v) {                 // Get the inputted text from EditText                 /*                     getText()                         Return the text the TextView is displaying.                  */                 String country = et.getText().toString();                 tv.setText("Your inputted country is : " + country);             }         });  } } 


More android examples

Komentar