开发者

Android views change on click

开发者 https://www.devze.com 2023-02-18 22:57 出处:网络
I need something simple, a开发者_高级运维n ability to switch views in my app like I do in iPhone,

I need something simple, a开发者_高级运维n 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());
    }
}

}


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)


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();


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号