Android edittext example code

           To get the values from the user that are enter by him to hold such type of value we need to use "EditText". In Android edittext is use to get the value from the user interface.

          In this article we will understand how to use textbox in application and what is the main purpose of it.

          Every Component use in Application define in layout xml file by default this file name is "activity_main.xml" or "main.xml" to user any control we need to place its code in that file. So for textview we are going to add the textview tag in xml fle -

File : res/layout/main.xml
 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
 
    <EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
 
        <requestFocus />
 
    </EditText>
 
</LinearLayout>
 
 
 
 Get or Set the values - 
 
 
package com.dheeruapps.android;
 
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.EditText;
import android.widget.Toast;
 
public class MyAcivity extends Activity {
 
 private EditText edittext; 
 String text;
 Button b;  
 
   
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);
   
       edittext = (EditText)findViewById(R.id.text);
       b = (Button)findViewById(R.id.button);
        
       text = edittext.getText();
  
       b.setOnClickListener(new OnClickListener(){
            public void onClick(View v) {
            // TODO Auto-generated method stub
                Toast.makeText(getApplicationContext(), text, Toast.LENGTH_LONG).show();
            }

        });

 }
}
 

0 comments:

Post a Comment

My Instagram