Android DialogFragment(2)



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);
    }
}


代码运行结果如图:

Android DialogFragment(2)_第1张图片



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


你可能感兴趣的:(android)