DialogFragment with interface to pass data back to activity

Thảo luận trong 'Điện thoại, viễn thông' bắt đầu bởi linhngok, 22/9/12.

  1. linhngok New Member

    linhngok

    Tham gia ngày:
    16/8/12
    Bài viết:
    23
    Đã được thích:
    7
    Điểm thành tích:
    0
    The articles "Create custom dialog with EditText" and "Pass back data from dialog to activity" introduced custom Dialog in pre-Honeycomb way.

    Honeycomb introduced Fragments to support reusing portions of UI and logic across multiple activities in an app. In parallel, the showDialog / dismissDialog methods in Activity are being deprecated in favor of DialogFragments. (reference: Android Developers Blog: Using DialogFragments)

    This article show how to modify last article "Pass back data from dialog to activity" to do the same function in DialogFragment way, with interface to pass data from DialogFragment to main activity.

    [​IMG]

    [​IMG]



    To use android.app.DialogFragment, android:minSdkVersion="11" is needed to be specified in AndroidManifest.xml.

    MyDialogFragment.java

    PHP:
    package com.AndroidCustomDialog;
     
    import android.app.DialogFragment;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;
     
    /*
     *  To use android.app.DialogFragment,
     *  android:minSdkVersion="11" is needed to be specified in AndroidManifest.xml
     */
    public class MyDialogFragment extends DialogFragment{
      
     
    TextView customDialog_TextView;
     
    EditText customDialog_EditText;
     
    Button customDialog_UpdatecustomDialog_Dismiss;
      
     static 
    MyDialogFragment newInstance() {
      return new 
    MyDialogFragment();
     }
     
     @
    Override
     
    public View onCreateView(LayoutInflater inflaterViewGroup container,
       
    Bundle savedInstanceState) {
      
    View dialogView inflater.inflate(R.layout.customlayoutcontainerfalse);
       
      
    customDialog_TextView = (TextView)dialogView.findViewById(R.id.dialogtextview);
      
    customDialog_Update = (Button)dialogView.findViewById(R.id.dialogupdate);
      
    customDialog_Dismiss = (Button)dialogView.findViewById(R.id.dialogdismiss);
      
    customDialog_Update.setOnClickListener(customDialog_UpdateOnClickListener);
      
    customDialog_Dismiss.setOnClickListener(customDialog_DismissOnClickListener);
       
      
    customDialog_EditText = (EditText)dialogView.findViewById(R.id.dialogedittext);
       
      return 
    dialogView;
     }
      
      
     private 
    Button.OnClickListener customDialog_UpdateOnClickListener
      
    = new Button.OnClickListener(){
      
      @
    Override
      
    public void onClick(View arg0) {
       
    // TODO Auto-generated method stub
       
    customDialog_TextView.setText(customDialog_EditText.getText().toString());
      } 
     };
     
     private 
    Button.OnClickListener customDialog_DismissOnClickListener
      
    = new Button.OnClickListener(){
      
      @
    Override
      
    public void onClick(View arg0) {
       
    // TODO Auto-generated method stub
       
    EditDialogListener activity = (EditDialogListenergetActivity();
                
    activity.updateResult(customDialog_EditText.getText().toString());
                 
       
    dismiss();
      }
     };
      
     public interface 
    EditDialogListener {
            
    void updateResult(String inputText);
        }
     
    }
    /res/layout/customlayout.xml.

    PHP:
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        
    android:id="@+id/customdialog"
        
    android:orientation="vertical"
        
    android:layout_width="fill_parent"
        
    android:layout_height="fill_parent"
        
    android:padding="10dp">
    <
    ImageView
        android
    :layout_width="wrap_content"
        
    android:layout_height="wrap_content"
        
    android:src="@drawable/ic_launcher"/>
    <
    TextView
        android
    :id="@+id/dialogtextview"
        
    android:layout_width="wrap_content"
        
    android:layout_height="wrap_content"/>
    <
    EditText
        android
    :id="@+id/dialogedittext"
        
    android:layout_width="fill_parent"
        
    android:layout_height="wrap_content"/>
    <
    Button
        android
    :id="@+id/dialogupdate"
        
    android:layout_width="fill_parent"
        
    android:layout_height="wrap_content"
        
    android:text="Update"/>
    <
    Button
        android
    :id="@+id/dialogdismiss"
        
    android:layout_width="fill_parent"
        
    android:layout_height="wrap_content"
        
    android:text="Dismiss"/>
    </
    LinearLayout>

    MainActivity.java

    PHP:
    package com.AndroidCustomDialog;
     
    import com.AndroidCustomDialog.MyDialogFragment.EditDialogListener;
    import android.os.Bundle;
    import android.app.Activity;
    import android.app.DialogFragment;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
     
    public class 
    MainActivity extends Activity implements EditDialogListener{
      
     
    String result "";
     
    TextView textReturned;
     
        @
    Override
        
    public void onCreate(Bundle savedInstanceState) {
            
    super.onCreate(savedInstanceState);
            
    setContentView(R.layout.activity_main);
             
            
    textReturned = (TextView)findViewById(R.id.textreturned);
             
            
    Button buttonStartDialog = (Button)findViewById(R.id.startdialog);
            
    buttonStartDialog.setOnClickListener(new Button.OnClickListener(){
              
             @
    Override
             
    public void onClick(View arg0) {
              
    // TODO Auto-generated method stub
              //showDialog(CUSTOM_DIALOG_ID);
               
              
    DialogFragment newFragment MyDialogFragment.newInstance();
                 
    newFragment.show(getFragmentManager(), "dialog");
                  
             }});
        }
     
     public 
    void updateResult(String inputText) {
      
    result inputText;
      
    textReturned.setText(result);
       
     }
         
    }
    /res/layout/activity_main.xml

    PHP:
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>
    <Button
        android:id="@+id/startdialog"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text=" Start Dialog "/>
    <TextView
        android:id="@+id/textreturned"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
    </LinearLayout>
     
    Đang tải...
  2. nguyenvanhung New Member

    nguyenvanhung

    Tham gia ngày:
    22/7/13
    Bài viết:
    22
    Đã được thích:
    0
    Điểm thành tích:
    0
    Giới tính:
    Nam
    cach nay cua ban cung giong nhu khi dung dialog binh thuong thui.ban co cach nao ma dung fragment binh thuong de gui data lai activity ko
     

Chia sẻ trang này