Pages

TextView, EditText dan Button Pada Android

Nama dan Alamat dimasukkan ke dalam EditText, kemudian ditekan button, maka akan ditampilkan dengan menggunakan TextView.


Buat project baru dengan mengikuti langkah-langkah berikut :

  1. Buka Eclipse
  2. Pada Eclipse File - New - Project - Android - Android Project - Next
  3. Kemudian pada jendela New Android Project isikan field berikut :
    1. Project Name : Hello World
    2. Build Target : Android 2.2
    3. Aplication name : Helo World
    4. Package Name : jhohannespurba.blogspot.com
    5. Create Activity : MainActivity
    6. Kemudian klik Finish  
  4. Buka main.xml (res - Layout - main.xml)
  5. Copy Source code berikut, lalu paste pada tab main.xml
    1. 1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      53
      54
      55
      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout 
       xmlns:android="http://schemas.android.com/apk/res/android"
       android:orientation="vertical" 
       android:layout_width="fill_parent"
       android:layout_height="fill_parent" 
       android:background="#FFF">
      
       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="HELLO"
        android:layout_gravity="center"
        android:textSize="40px" 
        android:textColor="#F34"></TextView>
       
       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="Masukkan Nama :"></TextView>
       
       <EditText  
        android:background="#F43" 
        android:layout_width="fill_parent" 
        android:width="100px" 
        android:layout_height="wrap_content" 
        android:id="@+id/textInputNama"></EditText>
       
       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="Masukkan Alamat :"></TextView>
       
       <EditText  
        android:background="#F43" 
        android:layout_width="fill_parent" 
        android:width="100px" 
        android:layout_height="wrap_content" 
        android:id="@+id/textInputAlamat"></EditText>
      
      
       <TextView 
        android:id="@+id/viewTampil" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:textColor="#000"
        android:text="test"></TextView>
       
       <Button 
        android:text="OK" 
        android:id="@+id/buttonOK" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"></Button>
      
      </LinearLayout>
      
  6. Kemudian buka MainActivity.java, Lalu ketikkan source code berikut :
    1. 1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      package jhohannespurba.blogspot.com;
      
      import android.app.Activity;
      import android.os.Bundle;
      import android.view.View;
      import android.view.View.OnClickListener;
      import android.widget.Button;
      import android.widget.EditText;
      import android.widget.TextView;
      
      public class mainActivity extends Activity {
       TextView tampil;
       EditText nama;
       EditText alamat;
       Button ok;
      
       @Override
       public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
      
        tampil = (TextView) findViewById(R.id.viewTampil);
        nama = (EditText) findViewById(R.id.textInputNama);
        alamat = (EditText) findViewById(R.id.textInputAlamat);
        ok = (Button) findViewById(R.id.buttonOK);
      
        ok.setOnClickListener(new OnClickListener() {
      
         @Override
         public void onClick(View arg0) {
          tampil.setText("Nama : " + nama.getText() + "\nAlamat :"
            + alamat.getText());
         }
        });
       }
      }
      
  7. Kemudian Jalankan dan Akan ditampilkan hasilnya seperti pada gambar berikut ini. Q2MUA3U9RC48
    Jhohannes H Purba Coding Sederhana May 21, 2012

    10 comments:

    1. gan kalo mau bikin sugges yang muncul otomatis kayak google gimanacaranya ya di eclipse?

      ReplyDelete
    2. CTRL + Spasi

      ketikkan misalnya "butt ", maka akan muncul otomatis gan...

      ReplyDelete
    3. tambah lagi tutorialnya dong gan...

      ReplyDelete
    4. kak, gimana yaa caranya kalo mau buat satu button yang fungsinya buat bikin edit text nya ga enable (ga bisa d klik untuk diedit atau diiputin apapun) ? mohon pencerahannya kak . . .

      ReplyDelete
    5. Hey keep posting such good and meaningful articles.

      ReplyDelete
    6. gimana caranya ketika kita sudah mengetikkan kalimat didalam kotak edit text, setelah di klik button. kotak edit text nya kembali kosong gan?

      ReplyDelete
    7. Great article, Thanks for your great information, the content is quiet interesting. I will be waiting for your next post.

      ReplyDelete
    8. What you're saying is completely true. I know that everybody must say the same thing, but I just think that you put it in a way that everyone can understand. I'm sure you'll reach so many people with what you've got to say.

      ReplyDelete
    9. I really appreciate the kind of topics you post here. Thanks for sharing us a great information that is actually helpful. Good day! Mobile Price In Pakistan

      ReplyDelete