2016-02-08 34 views
0

我有一个Activity,它会启动一个Fragmen t。在那个片段里我有一个EditText。当我得到用户输入的文本时,我想在interface的帮助下从我的Activity中获得该文本。我使用this guide使用接口的片段回调

在我MainActivity我执行的commentListener interfac e和我已经成功地得到onCommentEntered方法中的结果。但在doneListener,当用户按下完成活动的按钮时触发,我变得空。

显然onCommentEntereddoneListener之后运行。

关于如何在doneListener上得到结果的任何建议?

class MainActivity implements fragment.commentListener{ 

static String comment; 

protected void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.addtransaction); 
    done=(TextView)findViewById(R.id.done_button); 
} 

    // called when user presses a button and MainActivity finishes. 

public void doneListener(View v){ 
    // Here i get NULL 

    System.out.println(comment); 
    finish(); 
} 

@Override 
    public void onCommentEntered(String data) { 
      comment=data; 
      // Here i get what the user typed 
      System.out.println(comment); 
    } 
} 

我的片段

public class thefragment extends Fragment { 

commentListener cListener; 
static TextView note; 
EditText comment; 

public interface commentListener{ 
    void onCommentEntered(String data); 
} 

public static thefragment newInstance(){ 
    return new thefragment(); 
} 
public thefragment(){ 
} 


@Override 
public void onAttach(Context context) { 
    super.onAttach(context); 
    cListener=(commentListener) context; 
} 

@Override 
public void onDetach() { 
    super.onDetach(); 
    cListener=null; 
} 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
} 


@Nullable 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

    View v; 
    v=inflater.inflate(R.layout.fragment, container, false); 

    comment=(EditText)v.findViewById(R.id.comment_picker); 
    comment.setOnFocusChangeListener(new View.OnFocusChangeListener() { 
     @Override 
     public void onFocusChange(View v, boolean hasFocus) { 

      if (!hasFocus) { 
        cListener.onCommentEntered(comment.getText().toString()); 
      } 
     } 
    }); 
    return v; 
} 
} 
+0

发送View作为doneListener方法的参数的目的是什么? –

+0

我已经声明了我的xml布局上的doneListener作为按钮(android:onClick)的侦听器。 – tasgr86

+0

它看起来并不像你遵循指南。它使用java接口作为侦听器。所以如果你想让你的Activity成为你的监听器 - 它应该实现一个定义好的接口和你的片段来使用这个接口。 – gioravered

回答

0

创建一个新的接口,以及实现片段是相同的。在界面中创建一个方法并覆盖Fragments中的方法。在从Activity中调用片段时创建一个接口类型片段并调用接口方法。

如:

public class thefragment extends Fragment implement fragmentNofyInterface { 
    ... 
    @Override 
    protected void notify(String txt){ 
    mTvTxt.setText(txt); 
    } 
    ..... 
} 

Interface Format 

public interface fragmentNofyInterface { 
    protected void notify(String txt); 
} 

活动格式

class MainActivity implements fragment.commentListener{ 
     ..... 
     private fragmentNofyInterface mFragmentNotifier; 

     ......... 

     thefragment mFragment = new thefragment(); 
     mFragmentNotifier = (fragmentNofyInterface) mFragment; 
     FragmentTransaction transaction = mFragmentMngr.beginTransaction(). 
          add(R.id.rl_fragment_navigation_container, 
            mFragment); 
     transaction .commit(); 

     ...... 

     //Notify the fragment when you required 

     mFragmentNotifier.notify("hello world"); 


    } 
+0

这是与他所需要的相反。他希望活动听取片段 – gioravered

0

切换到addTextChangedListenerTextWatcherEditText,像this here, 似乎解决我的problem.Now onCommentEntered方法doneListener,因此被称为前字符串comment得到EditText中键入的任何内容。感谢大家的帮助。