Android Code For Textbox - Android

How to create textbox in android?

Snippet Code


  
Rate this page :
  [ 0 votes]

Text box is a field where the users is allowed to type text. The sample code given below is used to create a text box where users allowed to edit and type text using android.

MyAppActivity.java: ----------------------- package com.mkyong.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 MyAndroidAppActivity extends Activity { private EditText edittext; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); addKeyListener(); } public void addKeyListener() { edittext = (EditText) findViewById(R.id.editText); edittext.setOnKeyListener(new OnKeyListener() { public boolean onKey(View v, int keyCode, KeyEvent event) { if ((event.getAction() == KeyEvent.ACTION_DOWN) ampamp(keyCode == KeyEvent.KEYCODE_ENTER)) { Toast.makeText(MyAndroidAppActivity.this, edittext.getText(), Toast.LENGTH_LONG).show(); return true; } else if ((event.getAction() == KeyEvent.ACTION_DOWN) ampamp(keyCode == KeyEvent.KEYCODE_9)) { Toast.makeText(MyAndroidAppActivity.this, "Number 9 is pressed!", Toast.LENGTH_LONG).show(); return true; } return false; } }); } }

Tags


Ask Questions

Ask Question