如何使用 setcontent 通过按钮更改内容视图 [英] How to change the content view with a button using setcontent

查看:42
本文介绍了如何使用 setcontent 通过按钮更改内容视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过很多关于如何更改 android 屏幕的教程,但我无法理解/制作所有教程,有人可以帮助我完成我的项目.我有四个 xml 文件所有这些都有三个按钮,但只有两个将用于设置页面.我可以让应用程序加载任何其他页面(但无法建立按钮)(重要的一点).任何帮助表示赞赏.

I have seen many tutorials on how to change the screen for android yet i am not able to understand/make work all of them can some one help me with my project. i have four xml files all of which have three buttons but only two will be used to set the page. I can get the app to load any one of the other pages (but then cannot establish buttons)(the important bit). Any help is appreciated.

package com.store.shrek2;


import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {
int activityloaded = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    Button button1 = (Button) this.findViewById(R.id.button1);
    Button button2 = (Button) this.findViewById(R.id.button2);
    Button button3 = (Button) this.findViewById(R.id.button3);
    Button button4 = (Button) this.findViewById(R.id.button4);
    Button button5 = (Button) this.findViewById(R.id.button5);
    Button button6 = (Button) this.findViewById(R.id.button6);
    if(activityloaded == 1){
    button1.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
        }});
    button2.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            activityloaded = 2;
        }});
    button3.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            activityloaded = 4;
            setContentView(R.layout.activity4);
        }});
    }
    if(activityloaded == 2){
    setContentView(R.layout.activity2);
    button4.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            setContentView(R.layout.activity3);
        }});
    }
    }
 }



<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.store.shrek2"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="21" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".activity2"></activity>
    <activity android:name=".activity3"></activity>
    <activity android:name=".activity4"></activity>
</application>

</manifest>

推荐答案

请按照这些说明了解如何解决您的问题.

Please follow these instructions to get an idea on how to solve your problem.

  • 首先,创建一个活动 - 将其命名为 MainActivity
  • 其次,创建您的片段 - 取决于它们的数量:
  • 第三,为这些片段创建各自的 xml 文件.
  • 第四,假设您拥有的所有片段都包含完全不同类型的视图 - 如果没有,您可以重复使用片段并减少它们的数量.
  • 我认为您的按钮位于主视图中 - 您可以从中触摸它们以切换到新布局等.

由于您将使用您的主要活动来管理您的片段,

Since you will be using your main activity to manage your fragments,

  • 实现点击侦听器并将其附加到您的按钮上.
  • 其次,切换按钮的 id(使用 switch 语句),当点击给定按钮时,
  • 为各自的片段创建一个新实例并设置它,或者换句话说,加载它以查看(添加).您可以选择将其添加到 backstack,以便用户可以使用后退按钮轻松导航回它.
  • 当有人点击另一个按钮时,比如按钮 2,除了加载正确的片段外,执行与他们点击按钮 1 时相同的操作.
  • 使用片段的目的是能够重用它们.这也有助于您避免在 Activity 中做太多工作,因为 Fragment 可以自行完成它们需要的所有工作.
  • Implement a click listener and attach it to your buttons.
  • Secondly, switch your button's id (use a switch statement) and when a given button is clicked,
  • Create a new instance of your respective fragment and set it or in other words, load it to view (add). You can optionally add it to the backstack so that a user can easily navigate back to it using the back button.
  • When someone clicks another button, say button2, do the same thing as when they clicked button1 except load the correct fragment.
  • The point of using fragments is to be able to reuse them. That also helps you avoid doing too much work in your activities since fragments can do all they need on their own.

示例代码:MainActivity.java

public class MainActivity extends Activity implements View.OnClickListener
{
    private Button mLoadFragmentOne;
    private Button mLoadFragmentTwo;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
       super.onCreate(savedInstanceState);

       setContentView(R.layout.main);

       mLoadFragmentOne = (Button)findViewById(R.id.buttonOne);
       mLoadFragmentTwo = (Button)findViewById(R.id.buttonTwo);

       mLoadFragmentOne.setOnClickListener(this);
       mLoadFragmentTwo.setOnClickListener(this);

    }

    @Override
    public void onClick(View view)
    {
        switch(view.getId())
        {
           case R.id.buttonOne:
               FragmentOne fragmentOne = new FragmentOne();

               loadFragment(fragmentOne, "fragmentOne");

               break;
           case R.id.buttonTwo:
               FragmentTwo fragmentTwo = new FragmentTwo();

               loatFragment(fragmentTwo, "fragmentTwo");

               break;
           default:
               break;
        }
    }

    /**
    * This fragment container will be part of the main view.
    */
    public void loadFragment(Fragment frag, String tag)
    {
        FragmentManager fm = getFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();

        Fragment fragment = getFragmentManager().findFragmentById(R.id.fragmentContainer);
        if(fragment == null)
        {
           ft.add(R.id.fragmentContainer, frag, tag);
        } else 
        {
           ft.replace(R.id.fragmentContainer, frag, tag);
        }
       ft.addToBackStack(null);

       ft.commit();
    }
}

FragmentOne.java

public class FragmentOne extends Fragment
{
   private final String TAG = "com.example.app.FragmentOne";

   private Activity mActivity;

   public void onAttach(Activity act)
   {
     super.onAttach(act);

     this.mActivity = act;
   }

   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
   {
      View view = inflater.inflate(R.layout.fragment_one, container, false);

      //do whatever you want here - like adding a listview or anything

      return view;
   }
}

FragmentTwo.java

public class FragmentTwo extends Fragment
{
   private final String TAG = "com.example.app.FragmentTwo";

   private Activity mActivity;

   @Override
   public void onAttach(Activity act)
   {
      super.onAttach(act);

      this.mActivity = act;
   }

   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
   {
     View view = inflator.inflate(R.layout.fragment_two, container, false);

     //do whatever you want here - like set text to display in your fragment

     return view;
   }
}

现在,您只需使用此代码示例即可完成工作.

Now, you can simply use this code sample to get your job done.

这里的重点是创建一个容器——一个例子是主视图中的框架布局,给它一个 id 并记住这是你的片段将被加载的地方.

The point here is to create a container - an example would be a frame layout in your main view, give it an id and just remember that this is where your fragments will be loaded.

您将不得不添加其他内容,以便用户可以轻松地在视图之间来回导航.

You will have to add other stuff so that a user can easily navigate back and forth between views.

最后,请记住创建两个片段 xml 文件 - 它们可以包含您希望用户看到的任何内容 - 如图像、文本和列表或网格.这是你自己的选择.

Finally, remember to create the two fragment xml files - they can contain whatever you want your users to see - like images, texts and lists or grids. It is your own choice.

这就是我能抽出时间做的所有事情,希望对您有所帮助.

That is all I could spare time for and I hope it helps you.

如果您逐步按照这些说明进行操作,您应该能够解决问题.祝你好运.

If you follow these instructions step by step, you should be able to get your issue solved. Good luck.

这篇关于如何使用 setcontent 通过按钮更改内容视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆