커스텀 토스트, 커스텀 다이얼로그
2019. 10. 23. 15:31
Programing/Android
버튼 클릭 시 다이얼로그 창 띄어주고 데이터 입력.
확인 누르면 EditText 수정
취소하면 Toast 커스텀한 view가 보여짐
이하 소스코드
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:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:id="@+id/tvName"
android:layout_width="match_parent"
android:layout_height="70dp"
android:enabled="false"
android:hint="name" />
<EditText
android:id="@+id/tvEmail"
android:layout_width="match_parent"
android:layout_height="70dp"
android:enabled="false"
android:hint="email " />
<Button
android:id="@+id/btnClick"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:layout_marginRight="100dp"
android:layout_weight="1"
android:text="수정" />
</LinearLayout>
dialog.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:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="사용자 이름"
android:textSize="24sp" />
<EditText
android:id="@+id/edtName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="Name"
android:textSize="18sp" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="이메일"
android:textSize="24sp" />
<EditText
android:id="@+id/edtEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="Name"
android:textSize="18sp" />
</LinearLayout>
toast1.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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_red_light"
android:gravity="center_horizontal|center_vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@android:drawable/btn_star_big_on" />
<TextView
android:id="@+id/tvToast"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="취소됬습니다"
android:textSize="18sp" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
app:srcCompat="@android:drawable/btn_star_big_on" />
</LinearLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
Button btnClick;
View dialview, toastview;
TextView tvName,tvEmail, tvToast;
EditText edtEmail,edtName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnClick = (Button)findViewById(R.id.btnClick);
tvName = (TextView)findViewById(R.id.tvName);
tvEmail = (TextView)findViewById(R.id.tvEmail);
btnClick.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialview = (View)View.inflate(MainActivity.this,R.layout.dialog,null);
AlertDialog.Builder dial = new AlertDialog.Builder(MainActivity.this);
dial.setView(dialview);
dial.setPositiveButton("확인", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
edtName = (EditText)dialview.findViewById(R.id.edtName);
edtEmail = (EditText)dialview.findViewById(R.id.edtEmail);
tvName.setText(edtName.getText());
tvEmail.setText(edtEmail.getText());
}
});
dial.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.toast1,null);
tvToast = (TextView)toastview.findViewById(R.id.tvToast);
tvToast.setText("취소했습니다.");
toast.setView(toastview);
toast.show();
}
});
dial.show();
}
});
}
}
'Programing > Android' 카테고리의 다른 글
Adapter, GridView와 ListView 적용법 (0) | 2019.11.25 |
---|---|
FCM 앱 알람 (0) | 2019.11.02 |
배열과 테이블 위젯 이용한 계산기, 숫자버튼 예제 (0) | 2019.10.21 |
탭호스트 (0) | 2019.10.21 |
날짜 시간 위젯 (0) | 2019.10.21 |