AlertDialog in fragments

家住魔仙堡 提交于 2019-12-24 20:45:43

问题


I have some difficulties getting AlertDialog working in fragments. I have six buttons and when each button is clicked AlertDialog box should display its content. even though the prg is running without any error and tat fragment page is opening with the buttons if i click on a button dialogbox is not opening can anyone help me out? Thanks in advance.

Here is my fragment activity code:

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class Fragment3 extends  DialogFragment implements OnClickListener  {

    private static final int ALERT_DIALOG1 = 1;
  private static final int ALERT_DIALOG2 = 2;
  private static final int ALERT_DIALOG3 = 3;
  private static final int ALERT_DIALOG4 = 4;
  private static final int ALERT_DIALOG5 = 5;



  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
      //setContentView(R.layout.activity_fragment2);
    View view =  inflater.inflate(R.layout.fragment3layout, container, false);

      Button alert1 = (Button) view.findViewById(R.id.Button1);
      alert1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
          showDialog(ALERT_DIALOG1);
      }
  });



 default:
        dialog = null;
    }
    return dialog;

}

Here is my Manifest file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.pacificstereoapp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />


            </intent-filter>
        </activity>
        <activity
            android:name="com.example.a.Fragment1"
            android:label="@string/title_activity_fragment1" >
        </activity>

    </application>

</manifest>

回答1:


You are creating dialog, but not really showing it. Call dialog.show().



来源:https://stackoverflow.com/questions/14889174/alertdialog-in-fragments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!