简体   繁体   中英

How do I switch Activites/Layouts after clicking a button?

I'm trying to create buttons that will switch to different layouts/activities after clicking them. Can anyone assist?

package com.example.darsh.popup;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.Toast;

public class Main extends Activity {

private LayoutInflater inflater;
private PopupWindow pw;
private View popupView;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
popupView = inflater.inflate(R.layout.menu_layout, null, false);
}

public void showPopup(View view) {
pw = new PopupWindow(getApplicationContext());
pw.setTouchable(true);
pw.setFocusable(true);
pw.setOutsideTouchable(true);
pw.setTouchInterceptor(new OnTouchListener() {
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
            pw.dismiss();

            return true;
        }

        return false;
    }
});

pw.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
pw.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
pw.setOutsideTouchable(false);
pw.setContentView(popupView);
pw.showAsDropDown(view, 0, 0);

}

public void clickOne(View view) {
pw.dismiss();
Toast.makeText(getBaseContext(), "Link New User", Toast.LENGTH_SHORT)
        .show();

}

public void clickTwo(View view) {

pw.dismiss();
Toast.makeText(getBaseContext(), "Edit Core Device 1", Toast.LENGTH_SHORT)
        .show();
}

public void clickThree(View view) {
pw.dismiss();
Toast.makeText(getBaseContext(), "Delete Core Device 1", Toast.LENGTH_SHORT)
        .show();
} 

All I need to do is switch to LinkMenu.Java/linkmenu.xml after the user clicks "Link New User" , "Edit Core Device 1" , or "Delete Core Device 1" but I have no idea what to add to the current source code to do so.

Use Intent to switch to a different activity .

Intent intent = new Intent(Context, YourClass.class);
startActivity(intent);

activity_main.xml:

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="sendMessage"
    android:text="@string/button_send" />

MainActivity.java

/** Called when the user clicks the Send button */
public void sendMessage(View view) {
    Intent intent = new Intent(this, DisplayMessageActivity.class);
    EditText editText = (EditText) findViewById(R.id.edit_message);
    String message = editText.getText().toString()
    intent.putExtra(EXTRA_MESSAGE, message);
    startActivity(intent);
}

After the button is clicke sendMessage(View view) function is called. The function gets the text filed value and 'maps' it in the shared memory. The last line of the function creates and starts the new activity and the old one is no longer visible.

Hi try the following code :

    Button button = (Button) findViewById(R.id.button1);
    button.setOnClickListener(){    
    public void onCLick(View v){
        Intent i =new Intent(YouCurrentClass.this, NameOfsecondactivity.class);
        startActivity(i);
      }
    };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM