Android DialogFragment(2)

附录文章1简单介绍了如何实现一个DialogFragment,本文再介绍一种简单的方法:直接重写DialogFragment的onCreateDialog返回一个AlertDialog实现对话框。本文的例子和附录文章1不同的地方:不在依赖onCreateView。

代码运行逻辑简述:功能简单,当点击FloatingActionButton
后弹出一个自定义的DialogFragment,该DialogFragment重写了onCreateDialog,返回一个自定义的AlertDialog。

package zhangphil.app;import android.app.AlertDialog;import android.app.Dialog;import android.app.DialogFragment;import android.content.DialogInterface;import android.os.Bundle;import android.support.design.widget.FloatingActionButton;import android.support.v7.app.AppCompatActivity;import android.support.v7.widget.Toolbar;import android.view.LayoutInflater;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.TextView;public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);        setSupportActionBar(toolbar);        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);        fab.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                MyDialogFragment dialog = new MyDialogFragment();                dialog.show(getFragmentManager(), "zhangphil");            }        });    }    public static class MyDialogFragment extends DialogFragment {        @Override        public Dialog onCreateDialog(Bundle savedInstanceState) {            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());            LayoutInflater inflater = getActivity().getLayoutInflater();            View view = inflater.inflate(android.R.layout.simple_list_item_1, null);            TextView text = (TextView) view.findViewById(android.R.id.text1);            text.setText("zhang phil @ csdn");            builder.setView(view)                    .setPositiveButton("确定",                            new DialogInterface.OnClickListener() {                                @Override                                public void onClick(DialogInterface dialog, int id) {                                }                            }).setNegativeButton("取消", null);            return builder.create();        }    }    @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_main, 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);    }}


代码运行结果如图:



附录文章:
1,《Android DialogFragment(1)》链接地址:http://blog.csdn.net/zhangphil/article/details/50886077


更多相关文章

  1. 【Android】使用persist属性来调用脚本文件
  2. Android实现搜索保存历史记录功能
  3. Android(安卓)- shape不完整的圆环(ring)
  4. Android面试系列文章2018之内存管理篇
  5. Android-TextView用drawableLeft时,改变文字和图片间的距离
  6. [置顶] Android(安卓)JavaPoet 动态生成Java源码(1)
  7. (翻译) Android(安卓)Accounts Api使用指南
  8. Android实现整理PackageManager获取所有安装程序信息
  9. Android轻松实现多语言的方法示例

随机推荐

  1. Google Protocol Buffer
  2. Android仿IOS提示框
  3. Android(安卓)Kotlin项目集成阿里ARouter
  4. Android(安卓)OOM Adjustments
  5. Android(安卓)自动化测试框架-百度cafe
  6. Android自学——ListView
  7. android之MediaPlay播放视频
  8. Android(安卓)解屏幕锁与点亮屏幕(来电时
  9. android binder机制之三 Server Porxy
  10. 在Linux下编译Windows版本的adb和fastboo