0

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

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

我需要做的就是 LinkMenu.Java/linkmenu.xml在用户单击“链接新用户”“编辑核心设备 1”“删除核心设备 1”后切换到,但我不知道要在当前源代码中添加什么来这样做。

4

3 回答 3

2

使用 Intent 切换到不同的活动。

Intent intent = new Intent(Context, YourClass.class);
startActivity(intent);
于 2013-03-01T18:04:12.093 回答
1

活动主.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);
}

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

于 2013-03-01T18:20:01.600 回答
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);
      }
    };
于 2013-03-01T18:11:28.820 回答