MainActivity.java
package com.example.test;
import org.w3c.dom.Text;
import org.xml.sax.helpers.DefaultHandler;
import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
EditText nim,nama,alamat;
RadioGroup kel;
RadioButton jk;
Spinner agama;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.button1);
nim = (EditText)findViewById(R.id.editText1);
nama=(EditText)findViewById(R.id.editText2);
alamat=(EditText)findViewById(R.id.editText3);
kel=(RadioGroup)findViewById(R.id.radioGroup1);
agama=(Spinner)findViewById(R.id.spinner1);
// add button listener
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
int selectedId = kel.getCheckedRadioButtonId();
jk=(RadioButton) findViewById(selectedId);
Toast.makeText(getApplicationContext(),"NPM : "+ nim.getText()+"\n"+
"NAMA : "+nama.getText()+"\n"+"ALAMAT : "+ alamat.getText()+"\n"+
"JENIS KELAMIN : "+jk.getText()+
"\nAGAMA : "+String.valueOf(agama.getSelectedItem())+"\nData tersimpan...", Toast.LENGTH_LONG).show();
}
});
}
}
Kemudian ubah file activity_main.xml seperti koding dibawah ini.main_activity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView1"
android:layout_alignBottom="@+id/textView1"
android:layout_alignLeft="@+id/editText2"
android:ems="10" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText3"
android:layout_below="@+id/editText1"
android:layout_marginTop="16dp"
android:ems="10" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/editText2"
android:layout_alignBottom="@+id/editText2"
android:layout_alignLeft="@+id/textView1"
android:text="NAMA" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/textView2"
android:layout_marginTop="20dp"
android:text="NPM" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView3"
android:layout_below="@+id/editText2"
android:layout_marginTop="22dp"
android:text="ALAMAT" />
<EditText
android:id="@+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView4"
android:layout_alignBottom="@+id/textView4"
android:layout_alignLeft="@+id/radioGroup1"
android:ems="10" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView4"
android:layout_below="@+id/editText3"
android:layout_marginTop="26dp"
android:text="JENIS KELAMIN" />
<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView5"
android:layout_alignTop="@+id/spinner1"
android:layout_marginTop="14dp"
android:text="AGAMA" />
<Spinner
android:id="@+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/radioGroup1"
android:layout_below="@+id/radioGroup1"
android:entries="@array/country_arrays" />
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/textView5"
android:layout_marginLeft="32dp"
android:layout_toRightOf="@+id/textView5" >
<RadioButton
android:id="@+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="LAKI-LAKI" />
<RadioButton
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PEREMPUAN" />
</RadioGroup>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignRight="@+id/textView5"
android:layout_marginBottom="30dp"
android:text="Simpan" />
</RelativeLayout>
No comments:
Post a Comment