【问题标题】:How to make a View invisible/Gone onBackPressed() method in Android?如何在 Android 中使 View 不可见/Gone onBackPressed() 方法?
【发布时间】:2012-11-09 09:01:59
【问题描述】:

我在 Tabhost 中有 5 个活动。 (即 MainActivity.java Home.java、NowReading.java、Favorites.java 和 Profile.java)。在主要活动中,我正在加载 tabhost,如下所示。

public class MainActivity extends TabActivity {
    TabHost tabHost;
    Context context = MainActivity.this;
    TextView txt_Login;
    View homeTabLayout;
    View reviewLayout;

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

        txt_Login = (TextView) findViewById(R.id.txt_Login);
        final Spinner spnrLanguage = (Spinner) findViewById(R.id.spnrLanguage);
        final Spinner spnrBrowseBy = (Spinner) findViewById(R.id.spnrBrowseBy);

        tabHost = getTabHost();

        // Android tab
        Intent intentHome = new Intent().setClass(this, Home.class);
        TabSpec tabSpecHome = tabHost
                .newTabSpec("Home")
                .setIndicator("Home",
                        getResources().getDrawable(R.drawable.home))
                .setContent(intentHome);

        tabHost.addTab(tabSpecHome);

        Intent intentNowReading = new Intent().setClass(this, NowReading.class);
        TabSpec tabSpecNowReading = tabHost
                .newTabSpec("Now Reading")

                .setIndicator("Now Reading",
                        getResources().getDrawable(R.drawable.now_reading))
                .setContent(intentNowReading);
        tabHost.addTab(tabSpecNowReading);

        Intent intentFavourite = new Intent().setClass(this, Favorites.class);
        TabSpec tabSpecFavourite = tabHost
                .newTabSpec("Favourite")
                .setIndicator("Favorites",
                        getResources().getDrawable(R.drawable.favorites))
                .setContent(intentFavourite);
        tabHost.addTab(tabSpecFavourite);

        Intent intentProfile = new Intent().setClass(this, Profile.class);
        TabSpec tabSpecProfile = tabHost
                .newTabSpec("Profile")
                .setIndicator("Profile",
                        getResources().getDrawable(R.drawable.profile))
                .setContent(intentProfile);
        tabHost.addTab(tabSpecProfile);
tabHost.setCurrentTabByTag("Home");

在 Mainactivity.java 中将 Home.java 设置为当前选项卡。作为

tabHost.setCurrentTabByTag("Home");

在 setContentView 中加载布局 home_activity.xml。

这里是 XML 代码

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

    <RelativeLayout
        android:id="@+id/rel_HomeLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <GridView
            android:id="@+id/gridview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:numColumns="auto_fit"
            android:stretchMode="columnWidth" >
        </GridView>
    </RelativeLayout>

    <ScrollView
        android:id="@+id/scroll_ReviewLayout"
        android:layout_width="fill_parent"
        android:layout_height="match_parent" >

        <RelativeLayout
            android:id="@+id/rel_ReviewLayout01"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <ImageView
                android:id="@+id/img_OverVuBookCoverPhoto"
                android:layout_width="130dp"
                android:layout_height="165dp"
                android:layout_margin="5dp"
                android:adjustViewBounds="true"
                android:background="@drawable/rounded_image_borders"
                android:paddingLeft="5dp"
                android:paddingRight="5dp"
                android:src="@drawable/book1" />

            <TextView
                android:id="@+id/txt_revwBookTitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignTop="@+id/img_OverVuBookCoverPhoto"
                android:layout_marginTop="10dp"
                android:layout_toRightOf="@+id/img_OverVuBookCoverPhoto"
                android:text="The Basic Doctrine Of Islam"
                android:textColor="@color/White"
                android:textSize="12sp" />

            <TextView
                android:id="@+id/txt_revwAutherName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/txt_revwBookTitle"
                android:layout_marginTop="2dp"
                android:layout_toRightOf="@+id/img_OverVuBookCoverPhoto"
                android:text="By: Andrew J Newman"
                android:textColor="@color/SkyBlue"
                android:textSize="11sp" />

            <TextView
                android:id="@+id/txt_PublishDate"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/txt_revwAutherName"
                android:layout_marginTop="2dp"
                android:layout_toRightOf="@+id/img_OverVuBookCoverPhoto"
                android:text="Published: July, 30 2007"
                android:textColor="@color/DarkGray"
                android:textSize="11sp" />

            <RatingBar
                android:id="@+id/ratingbar"
                style="?android:attr/ratingBarStyleSmall"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/txt_PublishDate"
                android:layout_marginBottom="6dp"
                android:layout_marginTop="6dp"
                android:layout_toRightOf="@+id/img_OverVuBookCoverPhoto"
                android:isIndicator="false"
                android:numStars="5"
                android:rating="5"
                android:stepSize="1" />

            <Button
                android:id="@+id/btnOpenBook"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignBaseline="@+id/img_OverVuBookCoverPhoto"
                android:layout_below="@+id/ratingbar"
                android:layout_toRightOf="@+id/img_OverVuBookCoverPhoto"
                android:text="Open Book"
                android:textColor="@color/Black"
                android:textSize="13sp" />

            <ImageButton
                android:id="@+id/btnBack"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignBottom="@+id/btnOpenBook"
                android:layout_alignTop="@+id/btnOpenBook"
                android:layout_marginLeft="5dp"
                android:layout_toRightOf="@+id/btnOpenBook"
                android:src="@drawable/pic_btn_back"
                android:textColor="@color/Black"
                android:textSize="14sp" />

            <TextView
                android:id="@+id/txt_BookOverview"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@+id/img_OverVuBookCoverPhoto"
                android:layout_below="@+id/img_OverVuBookCoverPhoto"
                android:layout_marginBottom="10dp"
                android:layout_marginTop="20dp"
                android:text="Book Overview"
                android:textColor="@color/White"
                android:textSize="13sp" />

            <TextView
                android:id="@+id/txt_BookOverviewDetails"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@+id/img_OverVuBookCoverPhoto"
                android:layout_below="@+id/txt_BookOverview"
                android:lineSpacingExtra="4dp"
                android:lines="10"
                android:text="Lorem ipsum dolor sit amet, there was nothing that I can do she was dead, for this reasean I m gonna loose every thing consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat." >
            </TextView>
        </RelativeLayout>
    </ScrollView>

</FrameLayout>

在 Home.java 的 onCreate 方法中,我将 Framelayout 设为不可见/消失(其中包含 Cliked 书的概述)

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

        homeTabLayout = findViewById(R.id.rel_HomeLayout);
        reviewLayout = findViewById(R.id.scroll_ReviewLayout);
        reviewLayout.setVisibility(View.GONE);

现在,当我单击任何一本书时,它会使我之前在 onCreat 方法上使用代码不可见/消失的视图可见

gridView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
                    long arg3) {
                // TODO Auto-generated method stub

                Toast.makeText(context, BookTitle.get(pos), Toast.LENGTH_SHORT)
                        .show();
                reviewLayout.setVisibility(View.VISIBLE);

注意:这不是新的活动,而是我刚刚在点击事件上创建了一个视图(即 reviewLayout.setVisibility(View.VISIBLE);)。

  • 我想让这个视图在 onBackPressed() 方法上不可见/消失。
  • 我应该在 MainActivity.java 还是 Home.java 文件中调用此方法?
  • 如果我在 MainActivity.java 中调用此方法,则会引发 nullPointerException。

【问题讨论】:

  • 不用TabActivity,可以用Fragments..这样会更好
  • 无声杀手..!这次不可能了。请查看我的代码并建议我一个解决方案 plx
  • 哦,不,我们有一个方法调用 onBackPress(){},您可以在其中隐藏布局或视图..
  • 我知道这一点。这不是我的问题,伙计。我问了些别的事情

标签: android android-layout android-activity android-tabhost visibility


【解决方案1】:

您可以覆盖返回按钮方法

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        // You can call view.setVisiblility(View.GONE) if you want to remove it from the layout, or view.setVisibility(View.INVISIBLE) if you just want to hide it.
        view.setVisiblility(View.GONE); // or view.setVisibility(View.INVISIBLE);
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

【讨论】:

  • 感谢 rply 我应该在 MainActivit.java 还是 Home.java 中调用它?
  • 将此添加到您想要隐藏后退按钮视图的位置。
【解决方案2】:

您可以覆盖 BackPress 方法... 借助此功能,您可以隐藏您的布局或视图...

@Override
public void onBackPressed() {
     super.onBackPressed();
               // Invisible or Gone Your Views here....
    }

【讨论】:

  • @QadirHussain 在这里寻求建议你真的会得到一个很好的解决方案chat.stackoverflow.com/rooms/13215/…
  • @SilentKiller 我需要在 backpressed 中加载 10 个视图,但显示空指针异常,但是当我加载一个视图时它工作正常。你能告诉我如何同时加载多个视图
  • @Alliswell 可能是内存没有更多空间了,所以它删除了填充的对象,你得到 NPE,没有代码我无能为力。
  • @Alliswell 很高兴.. :)
  • @SilentKiller 我还有一个疑问。我们可以在屏幕上的不同位置放置列表视图吗?如果是的话,你能告诉我回家来做吗
猜你喜欢
  • 1970-01-01
  • 2016-06-24
  • 1970-01-01
  • 2015-08-30
  • 1970-01-01
  • 2016-09-17
  • 2015-03-06
  • 1970-01-01
  • 2017-07-16
相关资源
最近更新 更多