안 드 로 이 드 개발 은 팝 업 윈도 우 를 사용 하여 팝 업 경고 상자 의 재 활용 예 시 를 실현 합 니 다.

이 사례 는 안 드 로 이 드 개발 이 팝 업 윈도 우 를 사용 하여 팝 업 경고 상 자 를 실현 하 는 재 활용 류 를 다 루 고 있다.여러분 께 참고 하도록 공유 하 겠 습 니 다.구체 적 으로 는 다음 과 같 습 니 다.
안 드 로 이 드 개발 에 서 는 다음 그림 에서 보 여 준 인터페이스 가 모두 낯 설 지 않다 고 믿 습 니 다.이 팝 업 상자 의 사용 빈도 도 매우 높 기 때문에 저 는 편리 한 팝 업 인터페이스 에 사 용 했 습 니 다.그리고 확정 또는 취소 후의 논 리 를 추상 적 인 방법 으로 사용자 스스로 실현 시 켜 개발 효율 을 크게 향상 시 켰 다.다음은 이 종류 입 니 다.

package com.***.popupwindow;

import ******;

public abstract class MyPopupWindow {

  private PopupWindow popupWindow;
  private Activity context;
  private String content;
  private String positiveWord = "  ";
  private String negativeWord = "  ";

  /**
   *     
   *
   * @param context
   */
  public MyPopupWindow(Activity context) {
    this.context = context;
  }

  /**
   *      
   */
  public void show() {
    View popView = View.inflate(context, R.layout.popup, null);
    popupWindow = new PopupWindow(context);
    popupWindow.setHeight(400);
    popupWindow.setWidth(700);
    popupWindow.setOutsideTouchable(true);
    popupWindow.setFocusable(true);
    popupWindow.setContentView(popView);
    popupWindow.showAtLocation(context.getWindow().getDecorView(), Gravity.CENTER, 0, 0);

    TextView tv_pop_text = (TextView) popView.findViewById(R.id.tv_pop_text);
    tv_pop_text.setText(content);

    Button bt_pop_sure = (Button) popView.findViewById(R.id.bt_pop_sure);
    bt_pop_sure.setText(positiveWord);
    bt_pop_sure.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        sureClick();
      }
    });

    Button bt_pop_cancel = (Button) popView.findViewById(R.id.bt_pop_cancel);
    bt_pop_cancel.setText(negativeWord);
    bt_pop_cancel.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        cancelClick();
      }
    });
  }

  /**
   *         
   */
  public abstract void sureClick();

  /**
   *         
   */
  public abstract void cancelClick();

  /**
   *          
   *
   * @param content
   */
  public void setContent(String content) {
    this.content = content;
  }

  /**
   *        
   *
   * @param positiveWord
   */
  public void setPositiveWord(String positiveWord) {
    this.positiveWord = positiveWord;
  }

  /**
   *        
   *
   * @param negativeWord
   */
  public void setNegativeWord(String negativeWord) {
    this.negativeWord = negativeWord;
  }

  /**
   *        
   */
  public void dismiss() {
    popupWindow.dismiss();
  }
}

팝 업 상자 에 사용 할 레이아웃 popup.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="@android:color/white"
       android:orientation="vertical">

  <TextView
    android:id="@+id/tv_pop_text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:gravity="center"/>

  <TextView
    android:layout_width="match_parent"
    android:layout_height="1px"
    android:background="@android:color/darker_gray"/>

  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <Button
      android:id="@+id/bt_pop_sure"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:background="@android:color/transparent"
      android:layout_weight="1"/>

    <TextView
      android:layout_width="1px"
      android:layout_height="match_parent"
      android:background="@android:color/darker_gray"/>

    <Button
      android:id="@+id/bt_pop_cancel"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:background="@android:color/transparent"
      android:layout_weight="1"/>
  </LinearLayout>

</LinearLayout>

다음은 간단하게 사용 하 겠 습 니 다.인터페이스 에 단 추 를 놓 고 단 추 를 누 르 면 경고 상자 가 팝 업 됩 니 다.코드 는 다음 과 같 습 니 다:

package com.toprs.popupwindow;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.SeekBar;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

  private PopupWindow popupWindow;

  private Button button;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    button = (Button) findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        MyPopupWindow myPopupWindow = new MyPopupWindow(MainActivity.this) {

          @Override
          public void sureClick() {
            Toast.makeText(MainActivity.this, "  ", Toast.LENGTH_SHORT).show();
          }

          @Override
          public void cancelClick() {
            Toast.makeText(MainActivity.this, "  ", Toast.LENGTH_SHORT).show();
          }
        };
        myPopupWindow.setContent("    ?");
        myPopupWindow.show();
      }
    });
  }
}

즉 다음 과 같은 효과:

So,앞으로 사용 하려 면 몇 마디 코드 만 간단하게 호출 하면 됩 니 다!
더 많은 안 드 로 이 드 관련 내용 에 관심 이 있 는 독자 들 은 본 사이트 의 주 제 를 볼 수 있다.
본 고 에서 말 한 것 이 여러분 의 안 드 로 이 드 프로 그래 밍 에 도움 이 되 기 를 바 랍 니 다.

좋은 웹페이지 즐겨찾기