How to switch between screens?

后端 未结 6 1194
梦如初夏
梦如初夏 2020-12-31 20:37

I\'m new in the android develop world. I created simple application and created a simple GUI with one button. If the user presses this button, I want to change the screen

相关标签:
6条回答
  • 2020-12-31 20:47

    You Perform use this code:

    public class Activity1 extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        Button next = (Button) findViewById(R.id.Button01);
        next.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent myIntent = new Intent(view.getContext(), Activity2.class);
                startActivityForResult(myIntent, 0);
            }
    
        });
    }
    }
    

    activity 2:

    public class Activity2 extends Activity {
    
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main2);
    
        Button next = (Button) findViewById(R.id.Button02);
        next.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent intent = new Intent();
                setResult(RESULT_OK, intent);
                finish();
            }
    
        });
    }
    

    Also Make Sure to Create 2 different xml in Layout folder and add both Activity in Manifest File like

    <activity android:name=".Activity2"></activity>
    

    Hope this will help you.

    0 讨论(0)
  • 2020-12-31 21:08

    For moving from one Activity to another Activity, you need to use Intent.

    For example, you are having one activity "A" contains button and second activity "B", and you want to move from "A" to "B" then write:

    Intent intent = new Intent(A.this, B.class);
    startActivity(intent);
    

    Practical and informative examples for the Intent are here: Android Intents - Tutorial

    0 讨论(0)
  • 2020-12-31 21:09

    Another way,Button in an XML graphical layout and implement a button click listener together with an onclick method. The onclick method will start a new activity using an intent.

    <Button
        android:id="@+id/buttonClick"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Button" />
    

    Now go to your Class

    // Locate the button in activity_main.xml
        button = (Button) findViewById(R.id.buttonClick);
    
        // Capture button clicks
        button.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
    
                // Start NewActivity.class
                Intent myIntent = new Intent(getApplicationContext(),AddYourNewActivityName.class);
                startActivity(myIntent);
            }
        });
    

    Hope this helps .

    For details http://www.vogella.com/tutorials/AndroidIntent/article.html

    0 讨论(0)
  • 2020-12-31 21:09

    A better word than "Screen" is "Activity", You switch between Activities in android.

    A very simple way is to create a button on one activity (Lets call this First Activity) and assign it a method like onClick = startSecondActivity in the .xml file.

    Open FirstActivity.java, Add the method startSecondActivity inside the main method as shown below. This will work!

    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            // Set the content of the activity to use the activity_main.xml layout file
            setContentView(R.layout.activity_main);
        }
    
        public void startSecondActivity(View view){
            Intent i = new Intent(this, SecondActivity.class);
            startActivity(i);
        }
    }
    
    0 讨论(0)
  • 2020-12-31 21:12

    Set the button's onClick to a method that uses Intent.

    XML:

    <Button
        android:onClick="to_different_page"/>
    </Button>
    

    Java method:

    public void to_different_page(View view) {
        Intent intent = new Intent(this, browse_post_activity.class);
        startActivity(intent);
    }
    
    0 讨论(0)
  • 2020-12-31 21:13

    You can do something like this:

    import android.view.View;
    
    /** Called when the activity is first created. */
    public class YourActivity extends Activity implements View.OnClickListener {
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            Button button = (Button) findViewById(R.id.your_button_id);
            button.setOnClickListener(this);
        }
    
        public void onClick(View v) {
            Intent intent = new Intent(your_present_activity.this, target_activity.class);
            startActivity(intent);
        }
    }
    
    0 讨论(0)
提交回复
热议问题