屏幕到屏幕命令

时间:2012-07-11 15:24:38

标签: android xml button onclick screen

你是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>

5 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

有关按钮的网站上记录了此过程。谷歌搜索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标记中定义的方法,然后启动新活动或更新视图。无论你需要做什么

答案 2 :(得分:0)

你也可以做不同......

您可以在活动中实例化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上的基础知识。

答案 3 :(得分:0)

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

答案 4 :(得分:0)

你必须学习如何在Android中的活动/屏幕之间切换,你可以找到here为初学者准备好的教程

相关问题