屏幕到屏幕命令

问题描述:

您好新来的android和花了超过10小时寻找这个答案,但我似乎无法找到并理解它。我在主要的XML页面。我正在尝试创建一个转到另一个页面的按钮。我需要最简单最简单的方法来做到这一点。可以帮我吗?继承我的代码为我的主要xml。屏幕到屏幕命令

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:layout_centerVertical="true" 
    android:padding="@dimen/padding_medium" 
    android:text="@string/hello_world" 
    tools:context=".MainActivity" /> 

<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/textView1" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="66dp" 
    android:text="Button" /> 

</RelativeLayout> 
+1

http://developer.android.com/training/basics/firstapp/starting-activity.html – 2012-07-11 15:26:55

+0

您可以节省时间链接@OvidiuLatcu张贴。总是去developer.android.com,如果你没有找到你的答案来这里,并搜索一些问题,如果仍然没有,发布一个问题。 – 2012-07-11 15:38:08

<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/textView1" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="66dp" 
    android:text="Button" /> 

转到您的活动初始化你的按钮

Button btn=(Button)findViewById(R.id.button1); 
// Register listener to button btn 
btn.setOnClickListener(new OnClickListener() { 

    public void onClick(View v) { 

      // your action 

      Intent newActivityIntent= new Intent(currentActivity.this, NewClass.class); 
      startActivity(newActivityIntent);  
     } 
}); 

该过程在网站上记录有关按钮。谷歌搜索的Android按钮

<Button 
android:id="@+id/button1" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_below="@+id/textView1" 
android:layout_centerHorizontal="true" 
android:layout_marginTop="66dp" 
android:text="Button" 
android:clickable="true" 
android:onClick"insertMethodNameHere" 
/> 

这将会给你打电话onClick标签定义的方法,然后开始一个新的活动或更新视图。无论你需要做的

你也可以做到这一点不同......

您可以在活动实例化一个Button

private Button button; 

里面的onCreate(Bundle bundle)方法你找到你的按钮,在定义此活动XML,这是你所使用的setContentView(R.layout.yourxml);一个:

button = (Button) findViewById(R.id.button);

然后你使用一个OnClickListener

button.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      // Perform action on click 
     } 
    }); 

里面的onClick方法,实例化一个Intent

Intent intent = new Intent(CurrentActivity.this, ActivityToGo.class);

CurrentActivity =一个你,和你ActivityToGo要加载的一个。

startActivity();

在Android上阅读here基础。

public class MyActivity extends Activity implements OnClickListener { 

    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     Button button = (Button) findViewById(R.id.button1); 
     button.setOnClickListener(this); 
    } 

    public void onClick(View v) { 
     // Use a switch on v.getId() if you have multiple clickable views. 
     // Since there's just one button, ... 
     Intent intent = new Intent(this, TargetActivity.class; 
     startActivity(intent); 
    } 
} 

你必须学会​​如何在Android的活动/屏幕之间进行切换,你可以找到here一个很好的解释教程初学者

+0

谢谢你们的帮助 – 2012-07-11 15:45:33