我想创建一个具有Main类和CustomPopup类的Android应用程序。
在主要活动中,用户可以在文本字段中输入消息,当按下“GENERATE POPUP”按钮时,消息将在屏幕上显示为弹出窗口。弹出窗口必须是可移动的(在屏幕上拖动)。
到目前为止我做了什么,我真的需要帮助代码。
自定义弹出类
public class CustomPopup extends PopupWindow {
private String message;
private Double anchorX;
private Double anchorY;
PopupWindow popup;
public CustomPopup(String message) {
super();
this.message = message;
}
public void showPopup(Activity context)
{
LayoutInflater inflater = (LayoutInflater)context.getSystemService
( Context.LAYOUT_INFLATER_SERVICE);
}
}
主要类
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText messageTxt = (EditText) findViewById(R.id.messageTxt);
Button generateBtn = (Button) findViewById(R.id.generateBtn);
String message = messageTxt.getText().toString();
final CustomPopup popup = new CustomPopup(message);
generateBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
popup.showPopup();
}
});
}
}
答案 0 :(得分:0)
我正在使用 PopupHelper 类,您可以使用此代码:
public class PopUpHelper {
static Dialog dialog;
public static void showDialog(Activity activity, PINConfirmButtonClickListener action) {
if (dialog != null && dialog.isShowing()) {
boolean isShowing = dialog.isShowing();
return;
}
dialog = new Dialog(activity, R.style.MyAlertDialogStyle);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.(your_dialog_layout);
dialog.setCanceledOnTouchOutside(true);
dialog.setCancelable(true);
dialog.show();
dialog.findViewById(R.id.cancel_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
action.onPositiveClicked()
"YOUR_ACTION_HERE"
}
});
dialog.findViewById(R.id.cancel_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
action.onNegative()
"YOUR_ACTION_HERE"
}
});
dialog.findViewById(R.id.close_dialog_rl).setOnClickListener(view -> action.onCloseButtonClicked(dialog));
dialog.findViewById(R.id.resend_code_tv).setOnClickListener(view -> action.onResendCodeClicked());
}
public interface TwoButtonClickListener {
void onPositiveClicked(Dialog dialog);
void onNegativeClicked(Dialog dialog);
}
}
MyDialogStyle:(添加到styles.xml)
<!-- Alert Dialog Style-->
<style name="MyAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<!-- Used for the border radius -->
<item name="android:bottomLeftRadius">5dp</item>
<item name="android:bottomRightRadius">5dp</item>
<item name="android:topRightRadius">5dp</item>
<item name="android:topLeftRadius">5dp</item>
<item name="android:autofilledHighlight" tools:targetApi="o">@color/transparent</item>
</style>
并在调用 Popup 的活动中:
PopUpHelper.showDialog(this, new PopUpHelper.TwoButtonClickListener() {
@Override
public void onPositiveClicked(Dialog dialog) {
dialog.dismiss();
startHomeActivity();
}
@Override
public void onNegativeClicked(Dialog dialog) {
dialog.dismiss();
}
});
如果背景透明不起作用将“android:background="@android:color/transparent”设置为对话框布局
以我的布局为例,但您可以删除字体和某些字段:
<?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="wrap_content"
android:layout_gravity="center"
android:background="@android:color/transparent"
android:gravity="center"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<RelativeLayout
android:id="@+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="12dp">
<androidx.cardview.widget.CardView
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="32.5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingHorizontal="25dp"
android:paddingVertical="20dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="70dp"
android:orientation="vertical"
android:paddingTop="15dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="@font/bold"
android:gravity="center"
android:lineSpacingExtra="4dp"
android:text="@string/add_to_cart"
android:textColor="@color/color_secondary"
android:textSize="18sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:fontFamily="@font/medium"
android:lineSpacingExtra="4dp"
android:gravity="center"
android:text="@string/product_added_to_cart"
android:textColor="@color/color_secondary_60"
android:textSize="14sp"/>
</LinearLayout>
<LinearLayout
android:layout_marginTop="40dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/action_button"
android:layout_width="match_parent"
android:layout_height="45dp"
android:background="@color/orange_light"
android:ellipsize="end"
android:fontFamily="@font/medium"
android:foreground="?selectableItemBackground"
android:gravity="center"
android:letterSpacing="0.05"
android:singleLine="true"
android:text="@string/add_to_cart"
android:textColor="@color/white"
android:textSize="16sp"/>
<TextView
android:layout_marginTop="10dp"
android:layout_gravity="center_horizontal"
android:id="@+id/cancel_button"
android:layout_width="wrap_content"
android:layout_height="45dp"
android:ellipsize="end"
android:fontFamily="@font/medium"
android:foreground="?selectableItemBackground"
android:gravity="center"
android:letterSpacing="0.05"
android:singleLine="true"
android:text="@string/cancel"
android:textColor="@color/color_secondary_60"
android:textSize="13sp"/>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
<RelativeLayout
android:id="@+id/close_dialog_rl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/card_view"
android:layout_centerHorizontal="true"
android:layout_marginTop="22dp"
android:background="@drawable/shape_circle_white">
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_centerInParent="true"
android:src="@drawable/ic_close"
app:tint="@color/white"/>
</RelativeLayout>
</RelativeLayout>
<ImageView
android:id="@+id/confetti_v"
android:layout_width="65dp"
android:layout_height="65dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="@drawable/shape_circle_orange"
android:padding="16dp"
android:src="@drawable/ic_check_white"
app:tint="@color/white"/>
</RelativeLayout>
</LinearLayout>