android: 如何使用 ViewPager 更改按钮点击时的布局?

android: How to change layout on buttonclick using ViewPager?

我正在开发 android 应用程序。我正在使用 ViewPager 来提供应用程序的一些演示。我想在单击按钮时将布局从寻呼机更改为另一个布局。我知道如何在不使用 viewpager 的情况下更改按钮单击的布局。有人可以帮助我吗?

这是我的一段代码

这是main中的onCreate方法activity:

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewpager = (ViewPager) findViewById(R.id.pager);
        PagerAdapter padapter = new PagerAdapter(getSupportFragmentManager());
        viewpager.setAdapter(padapter);
        Button next = (Button) findViewById(R.id.button);
        next.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent myIntent = new Intent(view.getContext(), LoginActivity.class);
                startActivityForResult(myIntent, 0);
            }

        });

    }

这是activity点击按钮后我想去的:

 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);


        WebView myWebView = (WebView) findViewById(R.id.webview);
        myWebView.loadUrl("http://www.sowo.sk");
    }

然后是布局,其中只有viewpager a 这是单个片段的样子:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitXY"
        android:id="@+id/imageView"
        android:src="@drawable/sli1" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Ďakujeme, že využívate naše služby."
        android:id="@+id/textView"
        android:minWidth="100dp"
        android:textSize="25dp"
        android:gravity="center"
        android:maxWidth="200dp"
        android:textColor="#ffffff"
        android:layout_below="@+id/textView2"
        android:layout_alignLeft="@+id/button"
        android:layout_alignStart="@+id/button"
        android:layout_alignRight="@+id/button2"
        android:layout_alignEnd="@+id/button2" />

    <ImageView
        android:layout_width="200dp"
        android:paddingTop="20dp"
        android:layout_height="wrap_content"
        android:id="@+id/imageView5"
        android:src="@drawable/logo"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="130dp"
        android:layout_height="wrap_content"
        android:textColor="#170c0c"
        android:background="#daa520"
        android:text="Prihlásiť"
        android:id="@+id/button"
        android:layout_margin="20dp"
        android:layout_alignParentBottom="true"
        android:clickable="true"
        />

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="130dp"
        android:layout_height="wrap_content"
        android:background="#daa520"
        android:textColor="#170c0c"
        android:text="Registrovať"
        android:id="@+id/button2"
        android:layout_margin="20dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="VITAJTE"
        android:textColor="#ffffff"
        android:textSize="35sp"
        android:layout_margin="20dp"
        android:id="@+id/textView2"
        android:layout_below="@+id/imageView5"
        android:layout_centerHorizontal="true" />

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="40dp" >
    <ImageView
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:layout_margin="5dp"
        android:id="@+id/imageView6"
        android:src="@drawable/set"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_gravity="right"/>


       </LinearLayout>


</RelativeLayout>

这是单击按钮后我要移动到的布局:

<?xml version="1.0" encoding="utf-8"?>
<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    />

编辑: 我忘了提供片段 class 代码:

public class FragmentTwo extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        return inflater.inflate(R.layout.fragment_one_layout,container,false);
    }

}

编辑 2:

我试过像这样更改片段,但没有帮助:

public class FragmentOne extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        Button next = (Button) inflater.inflate(R.layout.fragment_one_layout, container, false).findViewById(R.id.button);
        next.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent myIntent = new Intent(view.getContext(), LoginActivity.class);
                startActivityForResult(myIntent, 0);
            }
        });

        return inflater.inflate(R.layout.fragment_one_layout,container,false);
    }


}

这当然不行。因为首先我要加载 viewpager 布局,它会更改为片段布局,我想单击尚未加载的按钮。或者我只是认为,这就是问题所在。但是怎么解决呢?

提前致谢。

请使用此方式获取按钮的id。

public class FragmentOne extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View content = inflater.inflate(R.layout.fragment_one_layout,container,null)
        Button next = (Button)content.findViewById(R.id.button);
        next.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent myIntent = new Intent(view.getContext(), LoginActivity.class);
                startActivityForResult(myIntent, 0);
            }
        });

        return content;
    }


}