0
我創造了這樣一個對話框:如何在對話框單擊時打開片段?
我的對話片段:
package com.aaron.waller.mrpolitik;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
public class extendsDialog extends Dialog implements
android.view.View.OnClickListener {
public Activity c;
public Dialog d;
public ImageButton yes, no;
public extendsDialog(Activity a) {
super(a);
// TODO Auto-generated constructor stub
this.c = a;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.custom_dialog);
yes = (ImageButton) findViewById(R.id.btn_yes);
no = (ImageButton) findViewById(R.id.btn_no);
yes.setOnClickListener(this);
no.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_yes:
break;
case R.id.btn_no:
break;
default:
break;
}
dismiss();
}
}
現在我想,如果我點擊「btn_yes」片段,該片段打開。我試了一下這樣的:
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_yes:
FragmentManager fm = c.getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.parteienlayout, new SPD());
ft.addToBackStack("Tag");
ft.commit();
break;
case R.id.btn_no:
break;
default:
break;
}
dismiss();
}
但後來它說:「錯誤的第二個參數類型的‘新SPD’」
我也試圖把這個在我的MainActivity.java:
public void tocdu(){
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.customdialog, new CDU());
ft.addToBackStack("Tag");
ft.commit();
}
這對話框點擊:
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_yes:
((MainActivity)getContext()).tocdu();
break;
case R.id.btn_no:
break;
default:
break;
}
dismiss();
}
但它仍然無法正常工作。 我該如何存檔?