2013-03-01 115 views
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”但我没有了解要添加到当前源代码中的内容。

回答

2

使用意图切换到不同的活动。

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

我照建议... 公共无效clickOne(查看视图){ \t \t pw.dismiss(); \t \t Toast.makeText(getBaseContext(), “链接新用户”,Toast.LENGTH_SHORT) \t \t \t \t .show(); \t \t Intent intent = new Intent(getApplicationContext(),LinkMenu.class); \t \t startActivity(intent); \t} 但应用程序只是崩溃。 – 2013-03-01 18:11:28

+0

你有SecondActivity吗? – 2013-03-01 18:12:29

+1

@AmaniSwann您是否在'manifest'文件中添加了'LinkMenu'活动? – 2013-03-01 18:13:32

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); 
     } 
    }; 
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)函数被调用。该函数获取文本字段值并将其“映射”到共享内存中。该函数的最后一行创建并启动新的活动,旧的不再可见。

相关问题