Android 에서 Dialog 대화 상자 사용 소결

머리말
최근 에 시간 이 좀 비 었 기 때문에 Dialog 대화 상자 에 작은 매듭 을 사용 하 는 데 한 시간 이 걸 렸 습 니 다.기본 적 인 것 입 니 다.학습 에 도움 이 되 기 를 바 랍 니 다.큰 소 는 웹 페이지 를 직접 닫 으 십시오.만약 당신 이 신출내기 라면,당신 이 직접 코드 를 한 번 두 드 리 는 것 을 건의 합 니 다.
먼저 효과 보기:

대화 상자 에 작은 매듭 사용 하기
일반 대화 상자

AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setTitle("    ");//  
builder.setMessage("   ,    ");
builder.setIcon(R.mipmap.ic_launcher);
builder.create();
builder.show();

일반 대화 상자
2.취소 확인 대화 상자

builder.setTitle("       ");
builder.setMessage("        ");
builder.setIcon(R.mipmap.ic_launcher);
builder.setPositiveButton("  ", new DialogInterface.OnClickListener() {
 //      Positive
 @Override
 public void onClick(DialogInterface dialog, int which) {
 Toast.makeText(activity, "      ", Toast.LENGTH_SHORT).show();
 }
});
builder.setNegativeButton("  ", new DialogInterface.OnClickListener() {
 @Override
 public void onClick(DialogInterface dialog, int which) {
 Toast.makeText(activity, "      ", Toast.LENGTH_SHORT).show();
 }
});
builder.create().show();

취소 확인 대화 상자
3.다 중 단추 대화 상자

builder.setTitle("       ");
builder.setMessage("   ");
builder.setIcon(R.mipmap.ic_launcher);
builder.setPositiveButton("    ", new DialogInterface.OnClickListener() {

 @Override
 public void onClick(DialogInterface dialog, int which) {
 Toast.makeText(activity, "        ", Toast.LENGTH_SHORT).show();
 }
});
builder.setNeutralButton("  ", new DialogInterface.OnClickListener() {

 @Override
 public void onClick(DialogInterface dialog, int which) {
 Toast.makeText(activity, "   ", Toast.LENGTH_SHORT).show();
 }
});
builder.setNegativeButton("   ,     ", new DialogInterface.OnClickListener() {

 @Override
 public void onClick(DialogInterface dialog, int which) {
 Toast.makeText(activity, "    ", Toast.LENGTH_SHORT).show();
 }
});
builder.create().show();

다 중 단추 대화 상자
4.목록 대화 상자

final String arrItem[] = getResources().getStringArray(R.array.aikaifa);
builder.setItems(arrItem, new DialogInterface.OnClickListener() {

 @Override
 public void onClick(DialogInterface dialog, int which) {
 Toast.makeText(activity, "     " + arrItem[which], Toast.LENGTH_SHORT).show();
 }
});
builder.create().show();

목록 대화 상자
5.Adapter 가 있 는 대화 상자

builder.setTitle(" Adapter    ");
builder.setIcon(R.mipmap.ic_launcher);
final List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
int arrImg[] = {R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher,
 R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher,
 R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher};
for (int i = 0; i < arrImg.length; i++) {
 Map<String, Object> map = new HashMap<String, Object>();
 map.put("img", arrImg[i]);
 map.put("title", "   " + i);
 list.add(map);
}
SimpleAdapter adapter = new SimpleAdapter(activity, list, R.layout.list_item, new String[]{"img", "title"}, new int[]{R.id.iv, R.id.tv});
builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
 @Override
 public void onClick(DialogInterface dialog, int which) {
 Toast.makeText(activity, "    " + list.get(which).get("title").toString().trim(), Toast.LENGTH_SHORT).show();
 }
});
builder.create().show();

Adapter 가 있 는 대화 상자
6.단일 선택 대화 상자

builder.setTitle("     ");
builder.setIcon(R.mipmap.ic_launcher);
builder.setSingleChoiceItems(R.array.aikaifa, 0, new DialogInterface.OnClickListener() {

 @Override
 public void onClick(DialogInterface dialog, int which) {
 Toast.makeText(activity, which+"", Toast.LENGTH_SHORT).show();
 }
});
builder.setPositiveButton("  ", new DialogInterface.OnClickListener() {
 @Override
 public void onClick(DialogInterface dialog, int which) {

 }
});
builder.create().show();

단일 선택 대화 상자
7.다 중 선택 대화 상자

builder.setTitle("     ");
builder.setIcon(R.mipmap.ic_launcher);
builder.setMultiChoiceItems(R.array.aikaifa, null, new DialogInterface.OnMultiChoiceClickListener() {

 @Override
 public void onClick(DialogInterface dialog, int which, boolean isChecked) {
 Toast.makeText(activity, which+""+isChecked, Toast.LENGTH_SHORT).show();
 }
});
builder.create().show();

다 중 선택 대화 상자
8.날짜 대화 상자

 DatePickerDialog datePickerDialog=new DatePickerDialog(activity,
 new DatePickerDialog.OnDateSetListener() {

  @Override
  public void onDateSet(DatePicker view, int year, int monthOfYear,
 int dayOfMonth) {
Toast.makeText(activity,
 year+" "+(monthOfYear+1)+" "+dayOfMonth+" ", Toast.LENGTH_SHORT).show();
  }
 },
 2017, 02, 9);
datePickerDialog.show();

날짜 대화 상자
9.시간 대화 상자

TimePickerDialog timePickerDialog=new TimePickerDialog(activity,
 new TimePickerDialog.OnTimeSetListener() {

  @Override
  public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Toast.makeText(activity,
 hourOfDay+"  "+minute+"  ", Toast.LENGTH_SHORT).show();
  }
 },
 17, 49, true);
timePickerDialog.show();

시간 대화 상자
10.사용자 정의 대화 상자

View view= LayoutInflater.from(activity).inflate(R.layout.dialog_login, null);
builder.setView(view);
builder.create();
final EditText et_phone=(EditText)view.findViewById(R.id.et_phone);
final EditText et_password=(EditText)view.findViewById(R.id.et_password);
Button btn_submit=(Button)view.findViewById(R.id.btn_submit);
btn_submit.setOnClickListener( new View.OnClickListener() {
 @Override
 public void onClick(View view) {
 Toast.makeText(activity, "    :"+et_phone.getText().toString()+"      :"+et_password.getText().toString(), Toast.LENGTH_SHORT).show();
 }
});
builder.show();

사용자 정의 대화 상자
프로젝트 설계 xml
list_item.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"
 android:background="#f5f5f5"
 android:orientation="horizontal"
 android:padding="10dp">
 <ImageView
 android:id="@+id/iv"
 android:src="@mipmap/ic_launcher"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" />
 <TextView
 android:id="@+id/tv"
 android:text="  "
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" />
</LinearLayout>
dialog_login.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="#ffffff"
 android:orientation="vertical">

 <LinearLayout
 android:id="@+id/linearLayout"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_centerInParent="true"
 android:layout_margin="8dp"
 android:orientation="vertical"
 android:padding="5dp">

 <TextView
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_marginBottom="10dp"
  android:layout_marginTop="10dp"
  android:gravity="center_horizontal"
  android:text="      "
  android:textColor="#414141" />

 <EditText
  android:id="@+id/et_phone"
  android:layout_width="match_parent"
  android:layout_height="48dp"
  android:gravity="center_vertical"
  android:hint="       "
  android:inputType="number"
  android:maxLength="11"
  android:paddingLeft="10dp"
  android:textSize="14sp" />

 <LinearLayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_marginBottom="20dp"
  android:layout_marginTop="10dp">

  <EditText
  android:id="@+id/et_password"
  android:layout_width="0dp"
  android:layout_height="48dp"
  android:layout_weight="4"
  android:gravity="center_vertical"
  android:hint="        "
  android:inputType="number"
  android:maxLength="6"
  android:paddingLeft="10dp"
  android:textSize="14sp" />

  <TextView
  android:id="@+id/tv_get_code"
  android:layout_width="0dp"
  android:layout_height="48dp"
  android:layout_marginLeft="10dp"
  android:layout_weight="2"
  android:enabled="false"
  android:gravity="center"
  android:text="    "
  android:textColor="#000000" />
 </LinearLayout>

 <Button
  android:id="@+id/btn_submit"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:textColor="#000000"
  android:gravity="center"
  android:paddingBottom="10dp"
  android:paddingTop="10dp"
  android:text="  " />
 </LinearLayout>
</RelativeLayout>
원본 코드 다운로드
총결산
이상 은 이 글 의 전체 내용 입 니 다.본 논문 의 내용 이 여러분 의 안 드 로 이 드 개발 자 들 에 게 어느 정도 도움 이 되 기 를 바 랍 니 다.궁금 한 점 이 있 으 시 면 댓 글 을 남 겨 주 셔 서 저희 에 대한 지지 에 감 사 드 립 니 다.

좋은 웹페이지 즐겨찾기