dialogfragment怎么add到指定的view
答案:2 悬赏:20
解决时间 2021-01-30 17:59
- 提问者网友:夕夏残阳落幕
- 2021-01-30 03:45
dialogfragment怎么add到指定的view
最佳答案
- 二级知识专家网友:山鬼偶尔也合群
- 2021-01-30 04:05
在MyActivity中,通过optionsMenu来分别触发告警框和提示框的显示,代码如下:
public class MainActivity extends Activity{
//设置告警框、提示框和帮助框的dialog fragment的tag。
public final static String ALERT_DIALOG_TAG = "ALERT_DIALOG_TAG";
public final static String PROMPT_DIALOG_TAG = "PROMPT_DIALOG_TAG";
public final static String HELP_DIALOG_TAG = "HELP_DIALOG_TAG";
…... 略 : 设置UI和创建OptionsMenu ......
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.alter_dialog:
alterDialogTestCase();
break;
case R.id.prompt_dialog:
promptDialogTestCase();
default:
break;
}
return false;
}
private void alterDialogTestCase(){
AlterDialogFragment adf = AlterDialogFragment.newInstance("Alert", "This is the Alter Message for test!");
FragmentTransaction ft = getFragmentManager().beginTransaction();
adf.show(ft, ALERT_DIALOG_TAG);
}
private void promptDialogTestCase(){
PromptDialogFragment pdf = PromptDialogFragment.newInstance("This is a Prompt Dialog!");
FragmentTransaction ft = getFragmentManager().beginTransaction();
pdf.show(ft, PROMPT_DIALOG_TAG);
}
public void onDialogDone(String tag, boolean cancelled, CharSequence message) {
String s = tag + " responds with: " + message;
if(cancelled)
s = tag + " was cancelled by the user";
//Toast是没有button的信息框,在一定时间后消失,很适合用于debug。
Toast.makeText(this, s, Toast.LENGTH_LONG).show();
}
}
通过fragment实现dialog的好处是:activity配置改变(例如转向)进行重构的情况下,fragment管理器能够自动重够,恢复原来的状态,无需人工干预。
public class MainActivity extends Activity{
//设置告警框、提示框和帮助框的dialog fragment的tag。
public final static String ALERT_DIALOG_TAG = "ALERT_DIALOG_TAG";
public final static String PROMPT_DIALOG_TAG = "PROMPT_DIALOG_TAG";
public final static String HELP_DIALOG_TAG = "HELP_DIALOG_TAG";
…... 略 : 设置UI和创建OptionsMenu ......
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.alter_dialog:
alterDialogTestCase();
break;
case R.id.prompt_dialog:
promptDialogTestCase();
default:
break;
}
return false;
}
private void alterDialogTestCase(){
AlterDialogFragment adf = AlterDialogFragment.newInstance("Alert", "This is the Alter Message for test!");
FragmentTransaction ft = getFragmentManager().beginTransaction();
adf.show(ft, ALERT_DIALOG_TAG);
}
private void promptDialogTestCase(){
PromptDialogFragment pdf = PromptDialogFragment.newInstance("This is a Prompt Dialog!");
FragmentTransaction ft = getFragmentManager().beginTransaction();
pdf.show(ft, PROMPT_DIALOG_TAG);
}
public void onDialogDone(String tag, boolean cancelled, CharSequence message) {
String s = tag + " responds with: " + message;
if(cancelled)
s = tag + " was cancelled by the user";
//Toast是没有button的信息框,在一定时间后消失,很适合用于debug。
Toast.makeText(this, s, Toast.LENGTH_LONG).show();
}
}
通过fragment实现dialog的好处是:activity配置改变(例如转向)进行重构的情况下,fragment管理器能够自动重够,恢复原来的状态,无需人工干预。
全部回答
- 1楼网友:一池湖水
- 2021-01-30 04:42
Fragment是Android honeycomb 3.0新增的概念,在Android——Fragment介绍、Android
Fragment使用、Android FragmentManage
FragmentTransaction介绍中做了关于Fragment的详细介绍。这一片主要通过一个实例了解Fragment的使用。
先看一下布局文件(layout):
android:orientation=“horizontal” android:layout_width=“match_parent”
android:layout_height=“match_parent”>
class=“com.fragment.main.TitlesFragment”
android:id=“@+id/titles” android:layout_weight=“1″
android:layout_width=“0px” android:layout_height=“match_parent” />
android:layout_width=“0px” android:layout_height=“match_parent”
android:background=“?android:attr/detailsElementBackground” />
布局文件中使用了fragment标签和FrameLayout标签。Android Fragment使用
中介绍了2中嵌入Fragment的方法,这个实例中都用到,从布局文件看到有了fragment标签,这是一种使用方法,FrameLayout标签将会成为第二种加载fragment的载体view。
看一下程序实现(com.fragment.main.TitlesFragment):
public class TitlesFragment extends ListFragment {
int mCurCheckPosition = 0;
int mShownCheckPosition = -1;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setListAdapter(new ArrayAdapter(getActivity(),
android.R.layout.simple_list_item_activated_1,
Shakespeare.TITLES)); //使用静态数组填充列表
if (savedInstanceState != null) {
mCurCheckPosition = savedInstanceState.getInt(“curChoice”, 0);
mShownCheckPosition = savedInstanceState.getInt(“shownChoice”, -1);
}
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
showDetails(mCurCheckPosition);
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt(“curChoice”, mCurCheckPosition);
outState.putInt(“shownChoice”, mShownCheckPosition);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id)
{
showDetails(position);
}
void showDetails(int index) {
mCurCheckPosition = index;
getListView().setItemChecked(index, true);
if (mShownCheckPosition != mCurCheckPosition) {
DetailsFragment df = DetailsFragment.newInstance(index);
FragmentTransaction ft = getFragmentManager()
.beginTransaction();
ft.replace(R.id.details, df);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
mShownCheckPosition = index;
}
}
}
TitlesFragment
TitlesFragment继承自Fragment的子类ListFragment,使用了一个静态数组填充列表,重写了onListItemClick方法,showDetails方法展示ListView
item的详情。
5DetailsFragment df =
DetailsFragment.newInstance(index);//获取详情Fragment的实例
FragmentTransaction ft =
getFragmentManager().beginTransaction();//获取FragmentTransaction 实例
ft.replace(R.id.details, df); //使用DetailsFragment 的实例
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();//提交
这里就使用到了Android Fragment使用中介绍的第二种加载fragment的方法。看一下DetailsFragment :
public class DetailsFragment extends Fragment {
public static DetailsFragment newInstance(int index) {
DetailsFragment f = new DetailsFragment();
// Supply index input as an argument.
Bundle args = new Bundle();
args.putInt(“index”, index);
f.setArguments(args);
return f;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
return null;
}
ScrollView scroller = new ScrollView(getActivity());
TextView text = new TextView(getActivity());
int padding = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, 4, getActivity().getResources()
.getDisplayMetrics());
text.setPadding(padding, padding, padding, padding);
scroller.addView(text);
text.setText(Shakespeare.DIALOGUE[getArguments().getInt("index", 0)]);
return scroller;
}
}
DetailsFragment 中使用newInstance(int index)方法产生DetailsFragment
实例并接受整型参数,重载了onCreateView方法创建view。
这个例子基本完成了,主要介绍的是在3.0以后的使用方法,其实Fragment在SDK1.6之后就可以使用了,在1.6上使用需要借助
android-support-v4.jar包实现。android-support-v4.jar在:SDK根目录\extras\android
\compatibility\v4下可以找到,如果想了解Fragment在SDK1.6上怎么实现的请参考Fragment 在Android
SDK1.6上实现。
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯