- 안드로이드로 전화번호부에서 연락처를 추가하는 Activity 를 만들어 보자.
- 레이아웃 나누는 것을 이용하여 화면 구상 해보도록 한다.
- JAVA 소스
- xml 소스
- 실행화면
- 레이아웃 나누는 것을 이용하여 화면 구상 해보도록 한다.
- JAVA 소스
public class Ex04_RegistrationActivity extends Activity implements OnClickListener, OnCheckedChangeListener {
String sex="남자";
String hap;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button1 = (Button)this.findViewById(R.id.button1);
button1.setOnClickListener(this);
RadioGroup rg = (RadioGroup)this.findViewById(R.id.sex);
rg.setOnCheckedChangeListener(this);
}
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.getId()==R.id.button1){
//Toast.makeText(this, "추가되었습니다.", Toast.LENGTH_SHORT).show();
EditText ak = (EditText)this.findViewById(R.id.name);
hap=ak.getText().toString();
ak = (EditText)this.findViewById(R.id.sosok);
hap=hap+" "+ak.getText().toString(); // edittext의 사용자로부터의 입력값 가져오기.
ak = (EditText)this.findViewById(R.id.number);
hap=hap+" "+ak.getText().toString();
ak = (EditText)this.findViewById(R.id.phonenumber);
hap=hap+" "+ak.getText().toString();
ak = (EditText)this.findViewById(R.id.email);
hap=hap+" "+ak.getText().toString();
hap=hap+" "+sex;
Toast.makeText(this, hap, Toast.LENGTH_SHORT).show();
}
}
public void onCheckedChanged(RadioGroup arg0, int arg1) {
// TODO Auto-generated method stub
switch(arg1){
case R.id.sex1:
sex="남자";
break;
case R.id.sex2:
sex="여자";
break;
}
}
}- xml 소스
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="연락처 추가"
android:textSize="40px" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="4px"
android:text="이 름 " />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:orientation="horizontal" >
<EditText
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:hint="이름을 입력하세요 " />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="4px"
android:text="소 속 " />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:orientation="horizontal" >
<EditText
android:id="@+id/sosok"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:hint="소속을 입력하세요 " />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="4px"
android:text="전화번호" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:orientation="horizontal" >
<EditText
android:id="@+id/number"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:hint="전화번호을 입력하세요"
android:phoneNumber="true" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="4px"
android:text="휴대폰 " />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:orientation="horizontal" >
<EditText
android:id="@+id/phonenumber"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:hint="전화번호을 입력하세요"
android:phoneNumber="true" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="4px"
android:text="이메일 " />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:orientation="horizontal" >
<EditText
android:id="@+id/email"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:hint="이메일을 입력하세요 " />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="4px"
android:text="성 별 " />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal" >
<RadioGroup
android:id="@+id/sex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checkedButton="@+id/sex1"
android:orientation="horizontal" >
<RadioButton
android:id="@id/sex1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="남자" />
<RadioButton
android:id="@+id/sex2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="여자" />
</RadioGroup>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="" />
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="추가" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="" />
</LinearLayout>
</LinearLayout> - 실행화면
'JAVA > Android' 카테고리의 다른 글
안드로이드 Calulator(계산기) 예제 따라하기 (0) | 2012.01.03 |
---|---|
안드로이드 CustomTab 예제 따라하기 (3) | 2012.01.03 |
안드로이드 RelativeLayout 예제 따라하기 (0) | 2012.01.02 |
안드로이드 RadioGroup 예제 따라하기 (0) | 2011.12.29 |
안드로이드 LinearLayout 예제 따라하기 (0) | 2011.12.26 |