Advertisement
Shailrshah

[Android] Display User Input

Nov 4th, 2014
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.21 KB | None | 0 0
  1. ///////////////////////
  2. // MainActivity.java //
  3. //////////////////////
  4.  
  5. package com.example.displaydetails;
  6.  
  7. import android.support.v7.app.ActionBarActivity;
  8. import android.widget.*;
  9. import android.os.Bundle;
  10. import android.view.Menu;
  11. import android.view.MenuItem;
  12. import android.view.View;
  13.  
  14.  
  15. public class MainActivity extends ActionBarActivity {
  16.    
  17.    
  18.     EditText name, city, phone;
  19.     Button submit;
  20.     TextView output;
  21.    
  22.  
  23.     @Override
  24.     protected void onCreate(Bundle savedInstanceState) {
  25.         super.onCreate(savedInstanceState);
  26.         setContentView(R.layout.activity_main);
  27.        
  28.         name = (EditText) findViewById(R.id.name);
  29.         city = (EditText) findViewById(R.id.city);
  30.         phone = (EditText) findViewById(R.id.phone);
  31.         submit = (Button) findViewById(R.id.submit);
  32.         output = (TextView) findViewById(R.id.output);
  33.        
  34.         submit.setOnClickListener(new View.OnClickListener() {
  35.            
  36.             @Override
  37.             public void onClick(View v) {
  38.                 // TODO Auto-generated method stub
  39.                
  40.                 //op.setText("Hello World!");
  41.                 output.setText(name.getText().toString()+"\n"+city.getText().toString()+"\n"+phone.getText().toString());
  42.             }
  43.         });
  44.        
  45.     }
  46.  
  47.  
  48.     @Override
  49.     public boolean onCreateOptionsMenu(Menu menu) {
  50.         // Inflate the menu; this adds items to the action bar if it is present.
  51.         getMenuInflater().inflate(R.menu.main, menu);
  52.         return true;
  53.     }
  54.  
  55.     @Override
  56.     public boolean onOptionsItemSelected(MenuItem item) {
  57.         // Handle action bar item clicks here. The action bar will
  58.         // automatically handle clicks on the Home/Up button, so long
  59.         // as you specify a parent activity in AndroidManifest.xml.
  60.         int id = item.getItemId();
  61.         if (id == R.id.action_settings) {
  62.             return true;
  63.         }
  64.         return super.onOptionsItemSelected(item);
  65.     }
  66. }
  67.  
  68. ///////////////////////
  69. // activity_main.xml //
  70. //////////////////////
  71.  
  72. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  73.     xmlns:tools="http://schemas.android.com/tools"
  74.     android:layout_width="match_parent"
  75.     android:layout_height="match_parent"
  76.     android:paddingBottom="@dimen/activity_vertical_margin"
  77.     android:paddingLeft="@dimen/activity_horizontal_margin"
  78.     android:paddingRight="@dimen/activity_horizontal_margin"
  79.     android:paddingTop="@dimen/activity_vertical_margin"
  80.     tools:context="com.example.displaydetails.MainActivity" >
  81.  
  82.     <EditText
  83.         android:id="@+id/name"
  84.         android:layout_width="wrap_content"
  85.         android:layout_height="wrap_content"
  86.         android:layout_alignParentTop="true"
  87.         android:layout_centerHorizontal="true"
  88.         android:layout_marginTop="38dp"
  89.         android:ems="10"
  90.         android:hint="@string/name"
  91.         android:inputType="textPersonName" >
  92.  
  93.         <requestFocus />
  94.     </EditText>
  95.  
  96.     <EditText
  97.         android:id="@+id/city"
  98.         android:layout_width="wrap_content"
  99.         android:layout_height="wrap_content"
  100.         android:layout_alignRight="@+id/name"
  101.         android:layout_below="@+id/name"
  102.         android:layout_marginTop="31dp"
  103.         android:ems="10"
  104.         android:hint="@string/city" />
  105.  
  106.     <EditText
  107.         android:id="@+id/phone"
  108.         android:layout_width="wrap_content"
  109.         android:layout_height="wrap_content"
  110.         android:layout_below="@+id/city"
  111.         android:layout_centerHorizontal="true"
  112.         android:layout_marginTop="36dp"
  113.         android:ems="10"
  114.         android:hint="@string/phone"
  115.         android:inputType="number" />
  116.  
  117.     <Button
  118.         android:id="@+id/submit"
  119.         android:layout_width="wrap_content"
  120.         android:layout_height="wrap_content"
  121.         android:layout_below="@+id/phone"
  122.         android:layout_centerHorizontal="true"
  123.         android:layout_marginTop="27dp"
  124.         android:text="@string/abc_searchview_description_submit" />
  125.  
  126.     <TextView
  127.         android:id="@+id/output"
  128.         android:layout_width="wrap_content"
  129.         android:layout_height="wrap_content"
  130.         android:layout_alignTop="@+id/submit"
  131.         android:layout_toRightOf="@+id/submit"
  132.         android:text="@string/app_name" />
  133.  
  134. </RelativeLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement