일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- 웹 프레임워크
- 개인정보안전성
- 모듈러 연산
- 마감임박
- 무료교육
- 개인정보보호
- 포너블
- 개인정보보호위원회
- arrow function
- 호이스팅
- function scope
- Writeup
- pwnable.tw
- 개인정보보호교육
- 디오판투스 알고리즘
- 한국정보보호산업협회
- 가명정보처리
- package-lock.json
- 백엔드
- 덧셈 암호
- 동적타이핑
- 국가인적자원개발컨소시엄
- 곱셈 암호
- 유클리드_알고리즘
- 확장 유클리드 알고리즘
- package.json
- 한국산업인력공단
- 한국정보보호산업협회기자단
- 백엔드입문
- node.js
- Today
- Total
짱짱해커가 되고 싶은 나
07-3. 메뉴/토스트/다이얼로그 연습 본문
프로젝트1: 사용자 정보 입력 앱
<activity_main.xml>
- TextView 2개 : 사용자 이름, 이메일
- Button 1개 : 여기를 클릭 (버튼을 누르면 대화상자 출력)
<대화상자용 xml>
- TextView 2개
- EditText 2개
<대화상자 버튼>
- 취소(토스트: 취소했습니다)
- 확인(editText에 입력한게 activity_main.xml의 TextView에 들어가게)
<토스트용 xml>
- ImageView 2개 : 글자 옆 이미지
- TextView 1개
<activity_main.xml>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/main_user"
android:textSize="20dp"
android:text="사용자 이름"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/main_mail"
android:textSize="20dp"
android:text="이메일"/>
<androidx.appcompat.widget.AppCompatButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn"
android:text="여기를 클릭"
android:background="#f0f0f0"
android:textColor="#000000"/>
</LinearLayout>
<dlg.xml>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="사용자 이름"
android:textStyle="bold"
android:textSize="20dp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/dlg_id"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="이메일"
android:textSize="20dp"
android:textStyle="bold"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/dlg_mail"/>
</LinearLayout>
<toast.xml>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="center"
android:background="#ff0000"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher_foreground"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textStyle="bold"
android:text="TextView"
android:textSize="20dp"
android:id="@+id/toastText"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher_foreground"/>
</LinearLayout>
<MainActivity.java>
package com.example.message;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
TextView user, mail, toastText;
EditText input_user, input_mail;
Button btn;
View dlgView, toastView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
user = (TextView)findViewById(R.id.main_user);
mail = (TextView)findViewById(R.id.main_mail);
btn = (Button)findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dlgView = (View)View.inflate(MainActivity.this, R.layout.dlg, null);
AlertDialog.Builder dlg = new AlertDialog.Builder(MainActivity.this);
dlg.setTitle("사용자 정보 입력");
dlg.setIcon(R.drawable.people_foreground);
dlg.setView(dlgView);
dlg.setPositiveButton("확인", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
input_user = (EditText)dlgView.findViewById(R.id.dlg_id);
input_mail = (EditText)dlgView.findViewById(R.id.dlg_mail);
String input1 = input_user.getText().toString();
String input2 = input_mail.getText().toString();
if(!input1.trim().isEmpty()) user.setText(input1);
if(!input2.trim().isEmpty()) mail.setText(input2);
}
});
dlg.setNegativeButton("취소", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast toast = new Toast(MainActivity.this);
toastView = (View)View.inflate(MainActivity.this, R.layout.toast, null);
toastText = (TextView)toastView.findViewById(R.id.toastText);
toastText.setText("취소했습니다");
toast.setView(toastView);
toast.show();
}
});
dlg.show();
}
});
}
}
여기를 클릭 버튼을 누르면 다이얼로그 xml을 inflate하고, 취소 버튼을 누르면 토스트 xml을 inflate 해야하기 때문에 각각의 View를 만들어준다. 뷰를 만들었으면 뷰에 속한 위젯을 찾을 때 해당 뷰 내에서 찾아야 한다~
프로젝트2: 사용자 정보 입력 앱 수정
main_activity에 있는 내용이 대화상자에도 나타나도록 한다.
if(!user.getText().toString().equals("사용자 이름"))
input_user.setText(user.getText().toString());
if(!mail.getText().toString().equals("이메일"))
input_mail.setText(mail.getText().toString());
프로젝트3: 옵션 메뉴(Java로만)
옵션 메뉴 item3개 - 강아지, 고양이, 토끼 (선택하면 동물 사진 변경)
package com.example.toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
ImageView image;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0,1,0,"강아지");
menu.add(0,2,0,"고양이");
menu.add(0,3,0,"토끼");
return true;
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch(item.getItemId()){
case 1:
image.setImageResource(R.drawable.ic_launcher_foreground);
return true;
case 2:
image.setImageResource(R.drawable.ic_launcher_background);
return true;
case 3:
image.setImageResource(R.mipmap.ic_launcher_round);
return true;
}
return false;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image = (ImageView)findViewById(R.id.image);
}
}
이미지 다운받기 싫어서 그냥.. 있는 기본 이미지로 했다 ^^
그리고 교재에서는 옵션 메뉴가 아래에 뜨길레 어떻게 바꾸나 찾아봤더니 그냥 버전에 따라 다른거였다.
프로젝트4: 이미지 토스트 랜덤 위치
<activity_main.xml>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="토스트 보이기"
android:id="@+id/btn"/>
</LinearLayout>
<toast.xml>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/image"
android:src="@drawable/ic_launcher_foreground"/>
</LinearLayout>
<MainActivity.java>
package com.example.toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Button btn;
View toastView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button)findViewById(R.id.btn);
toastView = (View)View.inflate(MainActivity.this, R.layout.toast, null);
btn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Toast toast = new Toast(MainActivity.this);
toast.setView(toastView);
Display display = ((WindowManager)getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
int xOffset = (int)(Math.random() * display.getWidth());
int yOffset = (int)(Math.random() * display.getHeight());
toast.setGravity(Gravity.TOP|Gravity.LEFT, xOffset, yOffset);
toast.show();
}
});
}
}
프로젝트5: custom dialog
dialog에 title 분할선을 넣고 싶어서 애 좀 먹었다ㅜ
<activity_main.xml>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:padding="20dp"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/rGroup">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="강아지"
android:id="@+id/rBtn1"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="고양이"
android:id="@+id/rBtn2"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="토끼"
android:id="@+id/rBtn3"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="말"
android:id="@+id/rBtn4"/>
</RadioGroup>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn"
android:text="그림보기"/>
</LinearLayout>
<dlg.xml>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:textColor="#0000ff"
android:textSize="20dp"
android:paddingTop="25dp"
android:paddingLeft="10dp"
android:textStyle="bold"
android:background="@drawable/text"
android:layout_width="match_parent"
android:layout_height="80dp"
android:id="@+id/title"/>
<ImageView
android:layout_width="200dp"
android:layout_height="200dp"
android:id="@+id/image"/>
<androidx.appcompat.widget.AppCompatButton
android:background="@drawable/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="닫기"
android:layout_gravity="center"
android:id="@+id/dlg_btn"/>
</LinearLayout>
다이얼로그 타이틀로 divider를 사용하고 싶었는데 잘 안되서 그냥 아예 타이틀도 TextView를 사용했다.
그래서 텍스트 뷰에서 그냥 아래 선을 주는 방식으로 사용했다.
<text.xml>
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:top="-3dp"
android:left="-3dp"
android:right="-3dp">
<shape>
<stroke
android:width="3dp"
android:color="#0000ff"/>
</shape>
</item>
</layer-list>
<btn.xml>
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:bottom="-1dp"
android:left="-1dp"
android:right="-1dp">
<shape>
<solid
android:color="#ffffff"/>
<stroke
android:width="1dp"
android:color="@color/black"/>
</shape>
</item>
</layer-list>
<MainActivity.java>
package com.example.toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
RadioGroup rGroup;
Button btn;
View dialogView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rGroup = (RadioGroup)findViewById(R.id.rGroup);
btn = (Button)findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
dialogView = (View)View.inflate(MainActivity.this, R.layout.dlg, null);
AlertDialog.Builder dlg = new AlertDialog.Builder(MainActivity.this);
ImageView image = (ImageView)dialogView.findViewById(R.id.image);
Button dlg_btn = (Button)dialogView.findViewById(R.id.dlg_btn);
TextView title = (TextView)dialogView.findViewById(R.id.title);
switch (rGroup.getCheckedRadioButtonId()){
case R.id.rBtn1:
title.setText("강아지");
image.setImageResource(R.drawable.ic_launcher_foreground);
break;
case R.id.rBtn2:
title.setText("고양이");
image.setImageResource(R.drawable.ic_launcher_background);
break;
case R.id.rBtn3:
title.setText("토끼");
image.setImageResource(R.mipmap.ic_launcher);
break;
case R.id.rBtn4:
title.setText("말");
image.setImageResource(R.mipmap.ic_launcher_round);
break;
default:
Toast.makeText(MainActivity.this, "버튼을 선택하세요.", Toast.LENGTH_SHORT).show();
}
dlg_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
dlg.setView(dialogView);
final AlertDialog dialog = dlg.create();
dialog.show();
dlg_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
}
});
}
}
버튼을 눌렀을 때 대화창이 닫히도록 하기 위해서는 AlertDialog 가 필요서 Builder.create()를 이용해 만들었다.
'모바일' 카테고리의 다른 글
08-2. SD 카드 폴더/파일 처리 연습 (0) | 2021.02.22 |
---|---|
08-1. 파일 처리 (0) | 2021.02.22 |
07-2. 토스트 & 대화상자 (0) | 2021.02.20 |
07-1. 메뉴 (0) | 2021.02.20 |
06-4. 뷰 컨테이너 연습 (0) | 2021.02.19 |