Android 视图随点击而变化

发布于 2024-10-24 02:52:53 字数 1138 浏览 2 评论 0原文

我需要一些简单的东西,能够像在 iPhone 中一样在我的应用程序中切换视图, 例如,我使用按钮加载第一个屏幕,单击此按钮并转到下一个屏幕,并且可以返回(我们都在 iPhone UI 中看到“后退”按钮)。 我一直在尝试执行 startActivity(this,MySecondScreen.class) 但它崩溃了。

public class mainClass extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}

public void login_Click(View v) {
    // Perform action on click
    try{
    Intent i = new Intent(this, MainMenuTabs.class);
    startActivity(i);
    }
    catch(Exception ex)
    {
        Log.e("main",ex.toString());
    }
}

我的第二个

类是 TabActivity 扩展程序 公共类 pissedoff 扩展了 Activity {

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

public void login_Click(View v) {
    // Perform action on click
    try{
    Intent i = new Intent(this, MainMenuTabs.class);
    startActivity(i);
    }
    catch(Exception ex)
    {
        Log.e("main",ex.toString());
    }
}

}

I need something simple, an ability to switch views in my app like I do in iPhone,
for example I load first screen with button, click on this button and go next screen with an ability to go back ( We all see Back button in the iPhone UI ).
I've been trying to do startActivity(this,MySecondScreen.class) but it crashes.

public class mainClass extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}

public void login_Click(View v) {
    // Perform action on click
    try{
    Intent i = new Intent(this, MainMenuTabs.class);
    startActivity(i);
    }
    catch(Exception ex)
    {
        Log.e("main",ex.toString());
    }
}

}

and my second class is this which is a TabActivity extender
public class pissedoff extends Activity {

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

public void login_Click(View v) {
    // Perform action on click
    try{
    Intent i = new Intent(this, MainMenuTabs.class);
    startActivity(i);
    }
    catch(Exception ex)
    {
        Log.e("main",ex.toString());
    }
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

爱格式化 2024-10-31 02:52:53
Intent myIntent = new Intent(view.getContext(), YourNewClass.class); /** Class name here */
startActivityForResult(myIntent, 0);

它可能会崩溃,因为在你的 android 清单文件中还必须声明这样的活动:

<activity android:name="YourNewClass" android:label="YourNewClass"></activity>

或者使用内置的图形东西。 (Android 清单 -> 应用程序 -> 应用程序节点 -> 添加 -> Activity)

Intent myIntent = new Intent(view.getContext(), YourNewClass.class); /** Class name here */
startActivityForResult(myIntent, 0);

It might crash because in your android manifest file also have to declare activity like that:

<activity android:name="YourNewClass" android:label="YourNewClass"></activity>

Or use the built in grahpical thing. (Android manifest -> Application -> Application Nodes -> Add -> Activity)

戏舞 2024-10-31 02:52:53

在您的 XML 中,为每个视图创建一个文件(使其更易于使用),然后在您的 main:

<ViewFlipper
xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/vf">

    <include android:id="@+id/firstView" layout="@layout/first" />
    <include android:id="@+id/secondView" layout="@layout/second" />
    <include android:id="@+id/thirdView" layout="@layout/third" />
    <include android:id="@+id/fourthView" layout="@layout/fourth" />

</ViewFlipper>

在您的 java 代码中:

ViewFlipper vf;
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        vf = (ViewFlipper) findViewById(R.id.vf);

当您想使用下一个视图时,请使用 vf.showNext();

In Your XML, create a file for each view (makes it easier to work with), then in your main:

<ViewFlipper
xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/vf">

    <include android:id="@+id/firstView" layout="@layout/first" />
    <include android:id="@+id/secondView" layout="@layout/second" />
    <include android:id="@+id/thirdView" layout="@layout/third" />
    <include android:id="@+id/fourthView" layout="@layout/fourth" />

</ViewFlipper>

in your java code:

ViewFlipper vf;
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        vf = (ViewFlipper) findViewById(R.id.vf);

when you want to use the next view use vf.showNext();

酒与心事 2024-10-31 02:52:53

1.这就是您的主活动的外观。在布局中添加按钮。

        public class MainActivity extends AppCompatActivity {
        Button B;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                B = (Button) findViewById(R.id.button);
                B.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                Intent obj = new Intent(MainActivity.this, NextActivity.class);
                startActivity(obj);
                                         }

                                     }
                );
            }
        }
  1. 这就是您的下一个 Activity 的外观。使用 onBackpressed 方法。

     public class NextActivity 扩展 AppCompatActivity {
        @覆盖
        protected void onCreate(@Nullable Bundle savingInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_next);
    
    
        }
        @覆盖
        公共无效onBackPressed(){
            结束();
        }
    
    
    
    
    }
    

3.希望有帮助。

1.This is how your Main Activity should look.Add button in the layout.

        public class MainActivity extends AppCompatActivity {
        Button B;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                B = (Button) findViewById(R.id.button);
                B.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                Intent obj = new Intent(MainActivity.this, NextActivity.class);
                startActivity(obj);
                                         }

                                     }
                );
            }
        }
  1. This is how your Next Activity should look.Use onBackpressed Method.

        public class NextActivity extends AppCompatActivity {
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_next);
    
    
        }
        @Override
        public void onBackPressed() {
            finish();
        }
    
    
    
    
    }
    

3.Hope it helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文