【问题标题】:screen to screen command屏幕到屏幕命令
【发布时间】:2012-07-11 15:24:38
【问题描述】:

嗨,我是 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>

【问题讨论】:

  • 您可以通过转到@OvidiuLatcu 发布的链接来节省时间。总是去 developer.android.com 如果你没有找到你的答案来这里搜索一些问题,如果仍然没有,发布一个问题。

标签: android xml button onclick screen


【解决方案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);       
        }
});

【讨论】:

    【解决方案2】:

    你也可以做不同的...

    您可以在 Activity 中实例化 Button

    private Button button;
    

    onCreate(Bundle bundle) 方法中,您可以找到您的按钮,如此 Activity 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】:
      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】:

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

        【讨论】:

          【解决方案5】:

          此过程记录在有关按钮的网站上。谷歌搜索安卓按钮

          <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 标签中定义的方法,然后启动一个新活动或更新视图。无论你需要做什么

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2014-11-12
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多