你好,游客 登录 注册 搜索
背景:
阅读新闻

Android 官方推荐:DialogFragment 创建对话框

[日期:2015-08-08] 来源:Linux社区  作者:lmj623565791 [字体:大 中 小]

5、传递数据给Activity

从dialog传递数据给Activity,可以使用“fragment interface pattern”的方式,下面通过一个改造上面的登录框来展示这种模式。

改动比较小,直接贴代码了:

package com.example.zhy_dialogfragment;

import Android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

public class LoginDialogFragment extends DialogFragment
{
 private EditText mUsername;
 private EditText mPassword;

 public interface LoginInputListener
 {
  void onLoginInputComplete(String username, String password);
 }
 
 @Override
 public Dialog onCreateDialog(Bundle savedInstanceState)
 {
  AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
  // Get the layout inflater
  LayoutInflater inflater = getActivity().getLayoutInflater();
  View view = inflater.inflate(R.layout.fragment_login_dialog, null);
  mUsername = (EditText) view.findViewById(R.id.id_txt_username);
  mPassword = (EditText) view.findViewById(R.id.id_txt_password);
  // Inflate and set the layout for the dialog
  // Pass null as the parent view because its going in the dialog layout
  builder.setView(view)
    // Add action buttons
    .setPositiveButton("Sign in",
      new DialogInterface.OnClickListener()
      {
       @Override
       public void onClick(DialogInterface dialog, int id)
       {
        LoginInputListener listener = (LoginInputListener) getActivity();
        listener.onLoginInputComplete(mUsername
          .getText().toString(), mPassword
          .getText().toString());
       }
      }).setNegativeButton("Cancel", null);
  return builder.create();
 }
}

拿到username和password的引用,在点击登录的时候,把activity强转为我们自定义的接口:LoginInputListener,然后将用户输入的数据返回。

MainActivity中需要实现我们的接口LoginInputListener,实现我们的方法,就可以实现当用户点击登陆时,获得我们的帐号密码了:

c) MainActivity
package com.example.zhy_dialogfragment;

import com.example.zhy_dialogfragment.LoginDialogFragment.LoginInputListener;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity implements LoginInputListener
{

 @Override
 protected void onCreate(Bundle savedInstanceState)
 {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
 }

 

 public void showLoginDialog(View view)
 {
  LoginDialogFragment dialog = new LoginDialogFragment();
  dialog.show(getFragmentManager(), "loginDialog");

 }

 @Override
 public void onLoginInputComplete(String username, String password)
 {
  Toast.makeText(this, "帐号:" + username + ",  密码 :" + password,
    Toast.LENGTH_SHORT).show();
 }

}

效果:

6、DialogFragment做屏幕适配

我们希望,一个对话框在大屏幕上以对话框的形式展示,而小屏幕上则直接嵌入当前的Actvity中。这种效果的对话框,只能通过重写onCreateView实现。下面我们利用上面的EditNameDialogFragment来显示。

EditNameDialogFragment我们已经编写好了,直接在MainActivity中写调用

public void showDialogInDifferentScreen(View view)
 {
  FragmentManager fragmentManager = getFragmentManager();
  EditNameDialogFragment newFragment = new EditNameDialogFragment();

  boolean mIsLargeLayout = getResources().getBoolean(R.bool.large_layout) ;
  Log.e("TAG", mIsLargeLayout+"");
  if (mIsLargeLayout )
  {
   // The device is using a large layout, so show the fragment as a
   // dialog
   newFragment.show(fragmentManager, "dialog");
  } else
  {
   // The device is smaller, so show the fragment fullscreen
   FragmentTransaction transaction = fragmentManager
     .beginTransaction();
   // For a little polish, specify a transition animation
   transaction
     .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
   // To make it fullscreen, use the 'content' root view as the
   // container
   // for the fragment, which is always the root view for the activity
   transaction.replace(R.id.id_ly, newFragment)
     .commit();
  }
 }

可以看到,我们通过读取R.bool.large_layout,然后根据得到的布尔值,如果是大屏幕则直接以对话框显示,如果是小屏幕则嵌入我们的Activity布局中

这个R.bool.large_layout是我们定义的资源文件:

在默认的values下新建一个bools.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <bool name="large_layout">false</bool>

</resources>

然后在res下新建一个values-large,在values-large下再新建一个bools.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <bool name="large_layout">true</bool>

</resources>

最后测试:

Android 官方推荐 : DialogFragment 创建对话框Android 官方推荐 : DialogFragment 创建对话框

上边为模拟器,下边为我的手机~~~~~

linux
相关资讯       DialogFragment  Android创建对话框 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款