笔记如下
如图:
Video_2018-03-15_145040.gif
- 输入框是在activity中的,按钮在fragment中
主要代码
SoundFragment类
package com.chen.fragment;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
/**
* Created by HP on 2018/3/15.
*/
public class SoundFragment extends Fragment {
Button btn;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
//先将fragment声明layout文件,然后装换为一个view对象
View view = inflater.inflate(R.layout.soundfragment, null);
btn = (Button) view.findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//获得文本输入框内容,如何获得
//获得activity对象
//activity与fragment之间传输数据,用getActivity()可以阿获得当前activity实例
EditText ed_text = (EditText) getActivity().findViewById(R.id.ed_text);
String text = ed_text.getText().toString();
Toast.makeText(getActivity(), text, Toast.LENGTH_SHORT).show();
}
});
return view;
}
}
网友评论