标签:count tst 否则 col hang width 判断 app ima
一、activity向fragment的数值之间的传递
之间传递的关键函数fragemnt(自己创建的fragment去调用).setArgument(bundle) 和getArgument()
以下有许多注意事项:
MainActivity.java
public class MainActivity extends AppCompatActivity {
private FragmentManager manager;
private FragmentTransaction transition;
private EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = findViewById(R.id.edit_content);
manager = getSupportFragmentManager();
transition = manager.beginTransaction();
transition.add(R.id.line_content, new ResultFragment());
transition.commit();
}
public void SendMessage(View view){
String info = editText.getText().toString().trim();
ResultFragment result = new ResultFragment();
Bundle bundle = new Bundle();
bundle.putString("info",info);
result.setArguments(bundle);
//即使是全局对象也要重新得到transition的对象,否则会抛出异常,单步调试F7或者F8就可以知道了
manager = getSupportFragmentManager();
transition = manager.beginTransaction();
transition.replace(R.id.line_content,result);
transition.commit();
}
}
ResultFlagment.java
public class ResultFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.countfragment,null);
TextView textView = view.findViewById(R.id.textView);
Bundle bundle = getArguments();
//一定要对Bundle就行判断,否则会抛出bundle.getString()得不到字符串的异常
if(bundle != null)
{
String info = bundle.getString("info");
textView.setText(info);
}
return view;
}
}
二、fragmeng向activity传数值
注意事项:得到MyListner的对象一般在onCreate或者在onAttach()方法里面进行初始化
ResourceFragment.java
public class ResoureFragment extends Fragment {
private EditText editText;
private Button button;
private MyListner myListner;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myListner = (MyListner) getActivity();
Log.i("info111", "onCreate-------->myListner=="+myListner);
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.resource,null);
button = view.findViewById(R.id.button);
editText = view.findViewById(R.id.edit_content);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String s = editText.getText().toString().trim();
myListner.SendMessage(s);
}
});
return view;
}
public interface MyListner{
void SendMessage(String s);
}
}
MainActivity.java的讲解
public class MainActivity extends AppCompatActivity implements ResoureFragment.MyListner {
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.getText);
}
@Override
public void SendMessage(String s) {
textView.setText(s);
}
}
activity_main.xml文件
注意事项:
引用其他的fragment的时候一定加上它的属性ID,否则会报错
<fragment
android:id="@+id/line_exercise"
android:name="com.example.zhang.fragment_activity.ResoureFragment"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_marginTop="10dp" />
知识点的讲解:java的多态性
1.MainActivity extends AppCompatActivity implements ResoureFragment.MyListner
MainActivity是AppCompatActivity的子类,同时也是MyListner的子类
2.myListner = (MyListner) getActivity();
这一步的得到是fragment的当前的类的Activity的窗口,他又MyListner的子类,所以就出现回调函数这样的方法
举例说明:
A extent B
B b = new A();
其实b这个时候获得是A的对象,和myListner = (MyListner) getActivity()方法相近
三、fragment和fragment的之间的数值传递
Android------------fragment数据传递
标签:count tst 否则 col hang width 判断 app ima
原文地址:https://www.cnblogs.com/liunx1109/p/9904233.html