如何从Android上的片段弹出一个弹出窗口?

时间:2015-06-28 18:00:06

标签: android android-fragments popup

我有一个活动文件,它使用ViewPager来显示不同的页面 - 每个页面都是带有视频的片段。我想要一些页面有按钮,当我按下弹出窗口显示一些信息时 - 这可能吗?

我在下面尝试过以下代码: 这是我的片段java中的代码:

public class MassageandBeauty extends Fragment implements View.OnClickListener {
Button btnPopup;
showPopup showPopup;



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

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,  Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.first_fragment, container, false);
    btnPopup = (Button)v.findViewById(R.id.cavern);
    btnPopup.setOnClickListener(this);
    return v;
}



//@Override
public void onViewCreated(View v) {
    btnPopup = (Button)v.findViewById(R.id.cavern);
    btnPopup.setOnClickListener(this);
}




//@Override
@Override
public void onClick(View parent) {
    new showPopup(getActivity().getApplicationContext()).cavern(parent);
}

以下是我的弹出窗口中的代码:

public class showPopup extends ActionBarActivity {
Context ctx;

public showPopup(Context ctx){
    this.ctx = ctx;
}


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_popup_layout);
    Typeface myTypeface = Typeface.createFromAsset(getAssets(), "fonts/Raleway-Black.ttf");
    TextView myTextView = (TextView)findViewById(R.id.textViewtop);
    LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    myTextView.setTypeface(myTypeface);
}





public void cavern(View parent) {
    final PopupWindow popup = new PopupWindow(ctx);

    ViewGroup.LayoutParams para = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

    TextView tvMessage = new TextView(ctx);
    Button btnDismiss = new Button(ctx);

    tvMessage.setLayoutParams(para);


    btnDismiss.findViewById(R.id.close);
    btnDismiss.setLayoutParams(para);
    btnDismiss.setWidth(20);
    btnDismiss.setHeight(10);
    btnDismiss.setText("x");
    popup.setContentView(btnDismiss);

    popup.setWidth(1200);
    popup.setHeight(800);
    popup.showAtLocation(parent, Gravity.CENTER_HORIZONTAL, 10, 10);
    popup.update();

    btnDismiss.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            popup.dismiss();
        }
    });


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {




    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_popup_layout, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@Override
protected void attachBaseContext(Context newBase) {
    super.attachBaseContext(new CalligraphyContextWrapper(newBase));
}

}

这是我的PopUp的xml

LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:id="@+id/showpopup"
android:layout_height="fill_parent"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:background="#8000"
android:orientation="vertical"

tools:context="morzineappy.morzineappy.showPopup">



<TextView
    android:id="@+id/textViewtop"
    android:layout_marginTop="5dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="20dp"
    android:textColor="#FFF"
    android:textSize="30sp"
    android:text="The Cavern" />

<Button
    android:id="@+id/close"
    android:layout_marginBottom="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginLeft="1200dp"
    android:layout_gravity="left"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="x" />

<ImageView
    android:id="@+id/poster"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/cavern"
    android:layout_marginTop="20dp"
    android:layout_marginLeft="800dp"
    android:layout_marginRight="20dp"
    android:layout_gravity="right" />


    <TextView
        android:id="@+id/textViewtop"

        android:layout_width="800dp"
        android:layout_height="wrap_content"
        android:layout_marginRight="20dp"
        android:layout_marginTop="60dp"
        android:layout_marginLeft="20dp"

        style="@style/BusinessFont"
        android:text="Text"
      />

由于某种原因,它不会在弹出窗口中显示文本 - 我想将布局添加到弹出窗口中,但我不确定如何 - 任何想法?!

2 个答案:

答案 0 :(得分:0)

首先使用扩展DialogFragment创建弹出或消息对话框,然后单击按钮:

MyDialogFragment dialog = new MyDialogFragment(....);
dialog.show(getActivity().getSupportFragmentManager(), "name of fragment here");

答案 1 :(得分:0)

您的问题不是很明确,但我会假设您尝试使用AlertDialog之类的内容。通常在创建这些对象时,需要-(void) viewTap:(UITapGestureRecognizer *) gestureRecognizer { if(gestureRecognizer.state == UIGestureRecognizerStateEnded) { //All fingers are lifted. } } 个对象作为参数。在Activity中,您通常可以创建一个AlertDialog,如下所示:

Context

但我猜你的问题是因为你在一个片段中,AlertDialog d = new AlertDialog(this); 对象不是this。所以对你来说,更像是这样:

Context