2013-12-15 4 views
1

네 개의 버튼이있는 main.xml을 만든 다음 Activity를 만들었습니다. 여기서 첫 번째 버튼이 새로운 활동을 열 수있는 인 텐트를 작성하고 작동했습니다. 그런데 두 번째 버튼도 똑같이하고 싶었지만 첫 번째 버튼과 같지 않았습니다. 내 대답은 왜입니까? 같은 행동을 취해야하는 이유는 무엇입니까?두 번째 버튼이 작동하지 않지만 첫 번째 버튼은

MainActivity.java
<?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> 

: 여기서

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

번째 버튼 문제 시작 ...

여기

은 ..

main.xml에 코드이며

 //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

으로 main.xml에에서 버튼의 onClick 속성을 추가 할 수 있습니다. 시도한 것을 공유하고 예상대로 작동하지 않는 것을 정확히 알려 주실 수 있습니까? – 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 방법에, 당신은 작업 만 수행 버튼 1을 클릭하면 네 개의 버튼 중 하나를 클릭 할 경우 작업을 수행해야합니다

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

지금 활동에서 두 번째 버튼의 더 전화가 없습니다

<?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" /> 

관련 문제