2013-12-15 92 views
1

我创建了一个main.xml,其中我有4个按钮,然后创建了Activity,在那里我为第一个按钮编写了一个Intent,以打开一个新的活动并且它工作。但是,然后,我想为第二个按钮做同样的事情,但它没有像第一个那样做。我的答案是为什么?为什么我应该改变做同样的行动?第二个按钮不起作用,但第一个是

这里是代码..

的main.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 

    > 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/noua" /> 

    <Button 
     android:id="@+id/button2" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/zece" /> 

    <Button 
     android:id="@+id/button3" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/unspe" /> 

    <Button 
     android:id="@+id/button4" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/doispe" /> 


</LinearLayout> 

MainActivity.java:

package com.orar; 

import android.os.Bundle; 
import android.app.Activity; 
import android.content.Intent; 
import android.view.View; 
import android.view.View.OnClickListener; 

    //implement the OnClickListener interface 
    public class MainActivity extends Activity 
    implements OnClickListener { 
     /** Called when the activity is first created. */ 
     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 

      setContentView(R.layout.main); 

      //get the Button reference 
      //Button is a subclass of View 
      //buttonClick if from main.xml "@+id/buttonClick" 
      View btnClick = findViewById(R.id.button1); 
      //set event listener 
      btnClick.setOnClickListener(this); 
     } 

     //override the OnClickListener interface method 
     @Override 
     public void onClick(View arg0) { 
      if(arg0.getId() == R.id.button1){ 
       //define a new Intent for the second Activity 
       Intent intent = new Intent(this,SecondActivity.class); 
       //start the second Activity 
       this.startActivity(intent); 
      } 
     } 

从这里开始的第二个按钮和问题...

 //implement the OnClickListener interface 
     public class MainActivity extends Activity 
     implements OnClickListener { 
      /** Called when the activity is first created. */ 
      @Override 
      public void onCreate(Bundle savedInstanceState) { 
       super.onCreate(savedInstanceState); 

       setContentView(R.layout.main); 

       //get the Button reference 
       //Button is a subclass of View 
       //buttonClick if from main.xml "@+id/buttonClick" 
       View btnClick = findViewById(R.id.button2); 
       //set event listener 
       btnClick.setOnClickListener(this); 
      } 

      //override the OnClickListener interface method 
      @Override 
      public void onClick(View arg0) { 
       if(arg0.getId() == R.id.button2){ 
        //define a new Intent for the second Activity 
        Intent intent2 = new Intent(this,ThirdActivity.class); 
        //start the second Activity 
        this.startActivity(intent2); 
       } 
      } 
     } 
    } 

秒ondActivity.java:

package com.orar; 

import android.app.Activity; 
import android.os.Bundle; 

public class SecondActivity extends Activity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     this.setContentView(R.layout.second); 

    } 
} 

ThirdActivity.java:

package com.orar; 

import android.app.Activity; 
import android.os.Bundle; 

public class ThirdActivity extends Activity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     this.setContentView(R.layout.third); 

    } 
} 

second.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
<TextView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:text="This is the second Activity" 
/> 
</LinearLayout> 

third.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
<TextView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:text="This is the third Activity" 
/> 
</LinearLayout> 
+0

有一个在你的活动没有第二个按钮的电话。你可以分享你的尝试,并告诉我们什么不按预期工作? – gahfy

+0

我应该如何在我的活动中调用我的第二个按钮?我试图改变的东西,使第二个按钮开始一个新的活动,但我没有......我应该改变第一个按钮的代码,为第二个按钮做同样的动作? – user3104504

回答

2

你有很多的问题。

//buttonClick if from main.xml "@+id/buttonClick" 
View btnClick = findViewById(R.id.button1); 
//set event listener 
findViewById(R.id.button1).setOnClickListener(this); 

你应该为你的四个按钮做到这一点:首先,你只为你第一个按钮设置的点击收听

findViewById(R.id.button1).setOnClickListener(this); 
findViewById(R.id.button2).setOnClickListener(this); 
findViewById(R.id.button3).setOnClickListener(this); 
findViewById(R.id.button4).setOnClickListener(this); 

然后,第二个问题是关于您的onClick方法,你只执行行动如果按钮一被点击。如果单击四个按钮中的一个,您应该执行的操作:

@Override 
public void onClick(View arg0) { 
    // create a general intent 
    Intent intent = null; 
    // define an intent for all cases 
    switch(arg0.getId()){ 
     case R.id.button1: 
      // Setting intent for first button 
      intent = new Intent(this,SecondActivity.class); 
      break; 
     case R.id.button2: 
      // Setting intent for second button 
      intent = new Intent(this,ThirdActivity.class); 
      break; 
     case R.id.button3: 
      // Setting intent for third button 
      intent = new Intent(this,ThirdActivity.class); 
      break; 
     case R.id.button4: 
      // Setting intent for fourth button 
      intent = new Intent(this,ThirdActivity.class); 
      break; 
    } 
    // start the Activity selected at the end 
    this.startActivity(intent); 
} 
+0

但是在创建这个案例后,我需要做的每件事情都打开另一个活动不一样? – user3104504

+0

您可以更改onClick方法中每个按钮的案例内的文本。所以如果你想在点击按钮3时开始一个不同的活动,你可以在case R.id.button3下写一个新的意图:Intent intent = ...;打破;' – Loyalar

+0

我更新了我的回复。核实。 – gahfy

0

恰恰是你可以做的就是创建一个在您的主要活动命名onButtonPressed()方法。它将处理我们的按钮新闻事件。

public void onButtonPressed(View v) { 
    Intent startActivityIntent = null; 
    switch (v.getId()) { 

     case R.id.button1: 
      startActivityIntent = new Intent(this, SecondActivity.class); 
     break; 

     case R.id.button2: 
      startActivityIntent = new Intent(this, ThirdActivity.class); 
     break; 

     case R.id.button3: 
     break; 

     case R.id.button4: 
     break; 
    } 
    if(startActivityIntent != null) 
    startActivity(startActivityIntent); 
} 

现在在main.xml中添加按钮的onClick属性作为

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:onClick="onButtonPressed" > 

<Button 
    android:id="@+id/button1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/noua" 
    android:onClick="onButtonPressed" /> 

<Button 
    android:id="@+id/button2" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/zece" /> 

<Button 
    android:id="@+id/button3" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/unspe" 
    android:onClick="onButtonPressed" /> 

<Button 
    android:id="@+id/button4" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/doispe" 
    android:onClick="onButtonPressed" /> 

相关问题