繁体   English   中英

将LinearLayout活动链接到片段

[英]Linking LinearLayout Activity to a Fragment

我无法将MainActivity链接到任何片段。
这是我的MainActivity.java:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

Button btn1,btn2,btn3,btn4,btn5;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btn1=(Button)findViewById(R.id.btn1);
    btn2=(Button)findViewById(R.id.btn2);
    btn3=(Button)findViewById(R.id.btn3);
    btn4=(Button)findViewById(R.id.btn4);
    btn5=(Button)findViewById(R.id.btn5);

    btn1.setOnClickListener(this);
    btn2.setOnClickListener(this);
    btn3.setOnClickListener(this);
    btn4.setOnClickListener(this);
    btn5.setOnClickListener(this);
}

@Override
public void onClick(View v) {

    onItemSelected(v.getId());
    /*Fragment fragment=null;
    int id=v.getId();
    switch(id){
        case R.id.btn1:
            fragment=new Btn1Fragment();
            break;
        case R.id.btn2:
            fragment=new Btn2Fragment();
            break;
        case R.id.btn3:
            fragment=new Btn3Fragment();
            break;
        case R.id.btn4:
            fragment=new Btn4Fragment();
            break;
        case R.id.btn5:
            finish();
    }

    FragmentManager fm=getSupportFragmentManager();
    fm.beginTransaction().replace(R.id.main,fragment).commit();
    LinearLayout linear=(LinearLayout)findViewById(R.id.main);*/


}

private void onItemSelected(int id) {
    Fragment fragment=null;
    switch(id){
        case R.id.btn1:
            fragment=new Btn1Fragment();
            break;
        case R.id.btn2:
            fragment=new Btn2Fragment();
            break;
        case R.id.btn3:
            fragment=new Btn3Fragment();
            break;
        case R.id.btn4:
            fragment=new Btn4Fragment();
            break;
        case R.id.btn5:
            finish();
    }

    FragmentManager fm=getSupportFragmentManager();
    fm.beginTransaction().replace(R.id.main,fragment).addToBackStack(null).commit();
}

还有一个片段:

    public class Btn1Fragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view= inflater.inflate(R.layout.fragment_btn1,container,false);
    return view;
}

帮帮我。

您可以尝试以下代码:

     public class MainActivity extends AppCompatActivity implements View.OnClickListener {

        Button btn1,btn2,btn3,btn4,btn5;
         Fragment fragment;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            btn1=(Button)findViewById(R.id.btn1);
            btn2=(Button)findViewById(R.id.btn2);
            btn3=(Button)findViewById(R.id.btn3);
            btn4=(Button)findViewById(R.id.btn4);
            btn5=(Button)findViewById(R.id.btn5);

            btn1.setOnClickListener(this);
            btn2.setOnClickListener(this);
            btn3.setOnClickListener(this);
            btn4.setOnClickListener(this);
            btn5.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            switch(v.getId()){
                case R.id.btn1:
                    fragment=new Btn1Fragment();
                    break;
                case R.id.btn2:
                    fragment=new Btn2Fragment();
                    break;
                case R.id.btn3:
                    fragment=new Btn3Fragment();
                    break;
                case R.id.btn4:
                    fragment=new Btn4Fragment();
                    break;
                case R.id.btn5:
                    finish();
            }

FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.main, fragment);
transaction.addToBackStack(null);

// Commit the transaction
transaction.commit();
        }

     }

Btn1Fragment

   public class Btn1Fragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater,
        ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_btn1, null);
return v;
}

使用此xml。

   <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.apkglobal.projectclick.MainActivity"
>


<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/btn1"
    android:text="Black Screen"
    style="@style/textstyle"
    />

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/btn2"
    android:text="Flashing Colors"
    style="@style/textstyle"
    android:layout_below="@+id/btn1"/>

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/btn3"
    android:text="Back Button wali Screen"
    style="@style/textstyle"
    android:layout_below="@+id/btn2"/>
<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/btn4"
    android:text="Jaado Dekhlo"
    style="@style/textstyle"
    android:layout_below="@+id/btn3"/>

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/btn5"
    android:text="Exit"
    style="@style/textstyle"
    android:layout_below="@+id/btn4"/>


</LinearLayout>

    <LinearLayout
        android:id="@+id/main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
    />
</LinearLayout>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM