안 드 로 이 드 개발 은 팝 업 윈도 우 를 사용 하여 팝 업 경고 상자 의 재 활용 예 시 를 실현 합 니 다.
7210 단어 AndroidPopupWindow팝 업 경고 상자
안 드 로 이 드 개발 에 서 는 다음 그림 에서 보 여 준 인터페이스 가 모두 낯 설 지 않다 고 믿 습 니 다.이 팝 업 상자 의 사용 빈도 도 매우 높 기 때문에 저 는 편리 한 팝 업 인터페이스 에 사 용 했 습 니 다.그리고 확정 또는 취소 후의 논 리 를 추상 적 인 방법 으로 사용자 스스로 실현 시 켜 개발 효율 을 크게 향상 시 켰 다.다음은 이 종류 입 니 다.
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,앞으로 사용 하려 면 몇 마디 코드 만 간단하게 호출 하면 됩 니 다!
더 많은 안 드 로 이 드 관련 내용 에 관심 이 있 는 독자 들 은 본 사이트 의 주 제 를 볼 수 있다.
본 고 에서 말 한 것 이 여러분 의 안 드 로 이 드 프로 그래 밍 에 도움 이 되 기 를 바 랍 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Bitrise에서 배포 어플리케이션 설정 테스트하기이 글은 Bitrise 광고 달력의 23일째 글입니다. 자체 또는 당사 등에서 Bitrise 구축 서비스를 사용합니다. 그나저나 며칠 전 Bitrise User Group Meetup #3에서 아래 슬라이드를 발표했...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.