簡體   English   中英

如何使用確定按鈕關閉android彈出窗口

[英]How to dismiss an android pop up window using an okay button

我正在為一個學校項目制作應用程序,但我只停留在一個區域。 我想使用確定按鈕關閉彈出窗口。 如果您有任何建議,或者有更好的方法來實現彈出窗口,請與他人分享。

package xyz.ashraf.whoisdelasalle;

import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;

public class Pop extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.popwindow);

    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);

    int width = dm.widthPixels;
    int height = dm.heightPixels;

    getWindow().setLayout((int)(width*.8),(int)(height*.5));

}
}

Java代碼^

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ScrollView01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
<RelativeLayout
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:background="#FAFAFA">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="John Baptist de La Salle was a French priest, educational reformer, and founder of the Institute of the Brothers of the Christian Schools. He is a saint of the Roman Catholic Church and the patron saint of teachers."
    android:id="@+id/textView"
    android:background="#FAFAFA"
    android:textColor="#000000"
    android:layout_alignParentTop="true"
    android:layout_marginTop="15dp"
    android:layout_marginRight="15dp"
    android:layout_marginLeft="15dp"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="OK"
    android:id="@+id/okButton_who"
    android:textColor="#00E676"
    android:background="#FAFAFA"
    android:layout_below="@+id/textView"
    android:layout_alignRight="@+id/textView"
    android:layout_alignEnd="@+id/textView"
    android:layout_marginTop="50dp"/>

</RelativeLayout>
    </ScrollView>

XML ^

提前致謝!

不知道這是否行得通,但是您可以嘗試:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.popwindow);

    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);

    int width = dm.widthPixels;
    int height = dm.heightPixels;

    getWindow().setLayout((int)(width*.8),(int)(height*.5));

    Button okButton = (Button) findViewById(R.id.okButton_who);
    okButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
        }
    });

}

否則,您可以按照akadouri的建議使用Dialogs或DialogFragments。

看起來您在尋找DialogsDialogFragments

我猜您可以使用AlertDialog來做到這一點。

    AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);

    builder.setTitle(title);        // If you want a header title
    builder.setCancelable(false);   // If you want to stop user dismissing with back button
    builder.setMessage(message);    // Message to user

    //If you want custom layout (not needed)
    LayoutInflater inflater = mActivity.getLayoutInflater();
    View dialogView = inflater.inflate(R.layout.pin_dialog, null);
    builder.setView(dialogView)

    builder.setPositiveButton(positiveLable, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            //Do something
        }
    });

    builder.setNegativeButton(negativeLable, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            //Do something, or remove if you dont want negative button
        }
    });

    builder.show(); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM