单击按钮后如何切换“活动/布局”?

时间:2013-03-01 18:02:05

标签: java android android-layout adt android-menu

我正在尝试创建按钮,单击它们后将切换到不同的布局/活动。有人可以帮忙吗?

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

我需要做的就是在用户点击“链接新用户”“编辑核心设备1”LinkMenu.Java/linkmenu.xml >“删除核心设备1”但我不知道要添加到当前源代码中的内容。

3 个答案:

答案 0 :(得分:2)

使用Intent切换到其他活动。

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

答案 1 :(得分:1)

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

按钮是clicke sendMessage(View view)函数被调用后。该函数获取文本字段值并将其“映射”在共享内存中。函数的最后一行创建并启动新活动,旧活动不再可见。

答案 2 :(得分:0)

您可以尝试以下代码:

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