Berbagi iLmu dan pengaLaman
AlertDialog Android
Akhirnya ngeblog lagi...
Setelah dalam postingan yang sebelumnya saya membuat aplikasi di android yang paling sederhana(hello world), sekarang saya mencoba membuat aplikasi yang sedikit lebih sulit.
Sekarang saya membuat Alert Dialog yang mana fungsinya sama seperti JOptionPane Message Dialog di swing.
Berikut ini source kodenya :
1. Source code bagian xmlnya :
<?xml version="1.0" encoding="utf-8"?><AbsoluteLayout android:id="@+id/widget0"android:layout_width="fill_parent" android:layout_height="fill_parent"xmlns:android="http://schemas.android.com/apk/res/android"><TextView android:id="@+id/lblNama" android:layout_width="wrap_content"android:layout_height="wrap_content" android:text="Nama"android:layout_x="13px" android:layout_y="21px"></TextView><TextView android:id="@+id/lblAlamat" android:layout_width="wrap_content"android:layout_height="wrap_content" android:text="Alamat"android:layout_x="15px" android:layout_y="88px"></TextView><EditText android:id="@+id/txtNama" android:layout_width="180px"android:layout_height="wrap_content" android:textSize="18sp"android:layout_x="76px" android:layout_y="9px"></EditText><EditText android:id="@+id/txtAlamat" android:layout_width="180px"android:layout_height="wrap_content" android:textSize="18sp"android:layout_x="78px" android:layout_y="82px"></EditText><Button android:id="@+id/button" android:layout_width="wrap_content"android:layout_height="wrap_content" android:text="Clik Me!!!"android:layout_x="103px" android:layout_y="161px"></Button></AbsoluteLayout>
2.Source code bagian javanya
package AlertDialog.meruvian.org;import android.app.Activity;import android.app.AlertDialog;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 main extends Activity {private TextView lblNama;private TextView lblAlamat;private EditText txtNama;private EditText txtAlamat;private Button button;private void buttonOnclick(View v) {AlertDialog d = new AlertDialog.Builder(this).create();d.setMessage("Nama : " + txtNama.getText() + " \nAlamat : "+ txtAlamat.getText());d.setCanceledOnTouchOutside(true);d.show();}@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);lblNama = (TextView) findViewById(R.id.lblNama);lblAlamat = (TextView) findViewById(R.id.lblAlamat);txtNama = (EditText) findViewById(R.id.txtNama);txtAlamat = (EditText) findViewById(R.id.txtAlamat);button = (Button) findViewById(R.id.button);button.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {buttonOnclick(v);}});}}
Dan ketika dijalankan tampilannya akan seperti berikut:
Selamat mencoba dan sekian dulu postingan kali ini, maaf kalau ada yang kurang
.
Special thanks to Dian.
Posted at 09:05AM Agu 18, 2010 by Maruf Ali Sahbana in Java | Comments[0]