并在MainActivity内实现Fragment内的控件的方法
MainActivety.java
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private FragmentManager fragmentManager;
private FragmentTransaction fragmentTransaction;
private xxx x;
private vvv v;
private EditText xet ,vet;
private Button xbt, vbt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragmentManager = getFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
x = new xxx();
v = new vvv();
fragmentTransaction.add(R.id.vvv, v, "fv");
fragmentTransaction.add(R.id.xxx, x, "fx");
fragmentTransaction.commit();
}
@Override
protected void onStart() {
super.onStart();
xbt = (Button) x.getView().findViewById(R.id.xxxbutton);
vbt = (Button) v.getView().findViewById(R.id.vvvbutton);
xbt.setOnClickListener(this);
vbt.setOnClickListener(this);
}
@Override
public void onClick(View view) {
vet = (EditText) v.getView().findViewById(R.id.vvveditText);
xet = (EditText) x.getView().findViewById(R.id.xxxeditText);
switch(view.getId()){
case R.id.vvvbutton:
if(vet != null) {
vet.setText(xet.getText().toString());
System.out.println(vet);
}
break;
case R.id.xxxbutton:
if(xet != null) {
xet.setText(vet.getText().toString());
System.out.println(xet);
}
break;
default:
break;
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context="com.example.java.m1020a.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/xxx"
android:orientation="vertical"
></LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/vvv"
android:orientation="vertical"
></LinearLayout>
</LinearLayout>
vvv.java
xxx.java
public class vvv extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fvvv, null);
return v;
}
@Override
public void onPause() {
super.onPause();
}
}
fvvv.xml
fxxx.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="fv"
android:ems="10"
android:id="@+id/vvveditText" />
<Button
android:text="fvvv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/vvvbutton" />
</LinearLayout>
0 件のコメント:
コメントを投稿