Use AlertDialog to display text from EditText

Description

The following code shows how to use AlertDialog to display text from EditText.

Example


/*from   www.j  a  v a 2s . com*/

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <EditText xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/script"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />

    <Button xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/eval"
        android:test="OK"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />

</LinearLayout>

Java code


import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
//w  w  w.  j a v  a  2s . co  m
public class MainActivity extends Activity {
  
  @Override
  public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);

    Button btn=(Button)findViewById(R.id.eval);
    final EditText script=(EditText)findViewById(R.id.script);
    
    btn.setOnClickListener(new View.OnClickListener() {
      public void onClick(View view) {
        String src=script.getText().toString();
        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setTitle("Exception!")
             .setMessage(e.toString())
            .setPositiveButton("OK", null)
            .show();
      }
    });
  }
}
Use AlertDialog to display text from EditText




















Home »
  Android »
    Android UI »




UI Basics
Action Bar
Animation
Button
Canvas
CheckBox
Clock Date Picker
Dialog
EditText
Event
Fragment
Gesture
GridView
ImageView
Layout
ListView
Map
Menu
Model
OpenGL
ProgressBar
RadioButton
Spinner
Tab
TextView
Thread
Toast
Video
View
WebView