actvi
package com.example.dialogfragment;
import android.app.Activity;
import android.app.FragmentManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
//memo
//activyからdialogfragmentをよぶ方法。リスナーで
public class MainActivity extends Activity implements OnClickListener, DialogListener{
Button button;
FragmentManager manager;
//android.support.v4.app.FragmentManager manager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
manager = getFragmentManager();
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(this);
/* Fragment01 frag = new Fragment01();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.main, frag, "Fragment01");
ft.commit();*/
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
// 以前ゴチャゴチャやってたけどもっとシンプルに書けました。考慮不足で申し訳ありません!
CommonDialogFragment newFragment = CommonDialogFragment.newInstance(
"title", "this is message");
//CommonDialogFragment newFragment =new CommonDialogFragment();
newFragment.setDialogListener(this);
String[] Iteams={"test1","test2","test3","test4","test5"};
newFragment.setIteams(Iteams);
newFragment.show(manager, "CommonDialogFragment");
break;
default:
break;
}
}
@Override
public void onPositiveClick() {
Toast.makeText(this, "ok ボタンが押された", Toast.LENGTH_SHORT).show();
}
@Override
public void onNegativeClick() {
Toast.makeText(this, "cancel ボタンが押された", Toast.LENGTH_SHORT).show();
}
}
com.java
package com.example.dialogfragment;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
//import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class CommonDialogFragment extends DialogFragment implements OnClickListener,comDialogListener{
private DialogListener listener = null;
private Button findbutton ;
private TextView textView1 ;
private String[] Iteams;
public static CommonDialogFragment newInstance(String title, String message) {
CommonDialogFragment frag = new CommonDialogFragment();
Bundle bundle = new Bundle();
bundle.putString("title", title);
bundle.putString("message", message);
frag.setArguments(bundle);
return frag;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
String title = getArguments().getString("title");
String message = getArguments().getString("message");
LayoutInflater inflater = LayoutInflater.from(getActivity());
View createdialog = inflater.inflate(R.layout.createdialog, null);
findbutton = (Button)createdialog.findViewById(R.id.findbutton);
findbutton.setOnClickListener(this);
textView1 = (TextView)createdialog.findViewById(R.id.textView1);
return new AlertDialog.Builder(getActivity())
.setIcon(R.drawable.ic_launcher)
.setTitle(title)
.setMessage(message)
.setView(createdialog)
.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int whichButton) {
listener.onPositiveClick();
dismiss();
}
}
)
.setNegativeButton(android.R.string.cancel,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int whichButton) {
listener.onNegativeClick();
dismiss();
}
}
)
.create();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
/**
* リスナーを追加する
*
* @param listener
*/
public void setDialogListener(DialogListener listener) {
this.listener = listener;
}
/**
* リスナーを削除する
*/
public void removeDialogListener() {
this.listener = null;
}
public void setIteams(String[] Iteams) {
this.Iteams = Iteams;
}
@Override
public void onClick(View arg0) {
SelectDialogFragment newFragment = SelectDialogFragment.newInstance(
"title", "this is message");
newFragment.setDialogListener(this);
newFragment.show(getActivity().getFragmentManager(), "CommonDialogFragment");
}
@Override
public void onSetIteam(int whichButton) {
// Toast.makeText( getActivity(), Integer.toString(whichButton), Toast.LENGTH_SHORT ).show();
textView1.setText((Iteams[whichButton]));
}
}
sel.java
package com.example.dialogfragment;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
//import android.support.v4.app.DialogFragment;
import android.app.DialogFragment;
public class SelectDialogFragment extends DialogFragment implements DialogInterface.OnClickListener{
private comDialogListener listener = null;
public static SelectDialogFragment newInstance(String title, String message) {
SelectDialogFragment frag = new SelectDialogFragment();
Bundle bundle = new Bundle();
bundle.putString("title", title);
bundle.putString("message", message);
frag.setArguments(bundle);
return frag;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
String title = getArguments().getString("title");
String message = getArguments().getString("message");
final CharSequence[] items = {"item1", "item2", "item3"};
return new AlertDialog.Builder(getActivity())
//.setIcon(R.drawable.ic_launcher)
//.setTitle(title)
//.setMessage(message)
/* .setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int whichButton) {
listener.onSelectClick();
dismiss();
}
}
)*/
/* .setNegativeButton(android.R.string.cancel,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int whichButton) {
//listener.onNegativeClick();
dismiss();
}
}
)*/
/* .setItems(
items,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// リスト選択時の処理
// which は、選択されたアイテムのインデックス
}
})*/
.setItems(items,this)
.create();
}
/**
* リスナーを追加する
*
* @param listener
*/
public void setDialogListener(comDialogListener listener) {
this.listener = listener;
}
/**
* リスナーを削除する
*/
public void removeDialogListener() {
this.listener = null;
}
@Override
public void onClick(DialogInterface dialog, int whichButton) {
listener.onSetIteam(whichButton);
}
}
diag.interface
package com.example.dialogfragment;
import java.util.EventListener;
public interface DialogListener extends EventListener {
/**
* okボタンが押されたイベントを通知する
*/
public void onPositiveClick();
/**
* cancelボタンが押されたイベントを通知する
*/
public void onNegativeClick();
}
cominterface
package com.example.dialogfragment;
public interface comDialogListener {
/**
* okボタンが押されたイベントを通知する
*/
public void onSetIteam(int whichButton);
}
0 件のコメント:
コメントを投稿