2013-03-22 2 views
0

다른 XML 레이아웃을 연결하는 버튼 막대가 있지만 다른 버튼을 클릭 할 때 다른 사람에게 돌아 가지 않고 하나만 가질 수 있습니다. 죄송합니다, 번거 로움을 위해 사전에 초보자가 있습니다. 이것은 내 첫 번째 게시물이지만,이 사이트를 많이 참조하고 있습니다. 미리 감사드립니다.여러 xml 레이아웃을 참조하는 버튼 설정

activity_main.xml

<ScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <TableLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:shrinkColumns="*" 
    android:stretchColumns="*" 
    android:background="#6B1414"> 
    <TableRow 
     android:id="@+id/tableRow1" 
     android:layout_height="fill_parent" 
     android:layout_width="fill_parent"> 
     <Button 
      android:id="@+id/btn1" 
      android:text="@string/Str" 
      android:textStyle="bold" 
      android:layout_width="0dip" 
      android:layout_height="wrap_content" 
      android:padding="18dip" 
      android:layout_weight="1" 
      android:background="#424242" 
      android:textColor="#ffffff" 
      android:gravity="center"/> 
     <Button 
      android:id="@+id/btn2" 
      android:text="@string/Agl" 
      android:textStyle="bold" 
      android:layout_width="0dip" 
      android:layout_height="wrap_content" 
      android:padding="18dip" 
      android:layout_weight="1" 
      android:background="#424242" 
      android:textColor="#ffffff" 
      android:gravity="center"/> 
     <Button 
      android:id="@+id/btn3" 
      android:text="@string/Int" 
      android:textStyle="bold" 
      android:layout_width="0dip" 
      android:layout_height="wrap_content" 
      android:padding="18dip" 
      android:layout_weight="1" 
      android:background="#424242" 
      android:textColor="#ffffff" 
      android:gravity="center"/> 
     <Button 
      android:id="@+id/btn4" 
      android:text="@string/Misc" 
      android:textStyle="bold" 
      android:layout_width="0dip" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:background="#424242" 
      android:textColor="#ffffff" 
      android:padding="18dip"/> 
    </TableRow> 

MainActivity.java

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

public class MainActivity extends Activity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     Button btn1 =(Button)findViewById(R.id.btn1); 
     Button btn2 =(Button)findViewById(R.id.btn2); 
     Button btn3 =(Button)findViewById(R.id.btn3); 
     Button btn4 =(Button)findViewById(R.id.btn4); 

     btn1.setOnClickListener(new Button.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       Intent myIntent = new Intent(); 
       myIntent.setAction(Intent.ACTION_VIEW); 
       myIntent.setData(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI); 
       setContentView(R.layout.activity_main); 
       return; 
      } 
     }); 

     btn2.setOnClickListener(new Button.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       Intent myIntent = new Intent(); 
       myIntent.setAction(Intent.ACTION_VIEW); 
       myIntent.setData(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI); 
       setContentView(R.layout.agil_main); 
       return; 
      } 
     }); 


     btn3.setOnClickListener(new Button.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       Intent myIntent = new Intent(); 
       myIntent.setAction(Intent.ACTION_VIEW); 
       myIntent.setData(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI); 
       setContentView(R.layout.int_main); 
       return; 
      } 
     }); 


     btn4.setOnClickListener(new Button.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       Intent myIntent = new Intent(); 
       myIntent.setAction(Intent.ACTION_VIEW); 
       myIntent.setData(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI); 
       setContentView(R.layout.misc_main); 
       return; 
      } 
     }); 

}} 

업데이트. XML이 좋아 보인다. 나는 아직도 내가 전에 가지고 있었던 같은 문제로 붙어있다. 각 레이아웃에 대한 활동을 추가했지만 여전히 레이아웃을 순환하지 않는 것과 동일한 문제가 있습니다. 각 java 파일은 이와 동일하게 보입니다.

MainActivity.java (새의 .class 텐트와 갱신)

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

public class MainActivity extends Activity { 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    Button btn1 =(Button)findViewById(R.id.btn1); 
    Button btn2 =(Button)findViewById(R.id.btn2); 
    Button btn3 =(Button)findViewById(R.id.btn3); 
    Button btn4 =(Button)findViewById(R.id.btn4); 

    btn1.setOnClickListener(new Button.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      Intent myIntent = new Intent(); 
      myIntent = new Intent(getApplicationContext(), MainActivity.class); 
      myIntent.setAction(Intent.ACTION_VIEW); 
       myIntent.setData(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI); 
      setContentView(R.layout.activity_main); 
      return; 
     } 
    }); 

    btn2.setOnClickListener(new Button.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      Intent myIntent = new Intent(); 
      myIntent = new Intent(getApplicationContext(), MainAgil.class); 
      myIntent.setAction(Intent.ACTION_VIEW); 
      myIntent.setData(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI); 
      setContentView(R.layout.agil_main); 
      return; 
     } 
    }); 


    btn3.setOnClickListener(new Button.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      Intent myIntent = new Intent(); 
      myIntent = new Intent(getApplicationContext(), MainInt.class); 
      myIntent.setAction(Intent.ACTION_VIEW); 
      myIntent.setData(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI); 
      setContentView(R.layout.int_main); 
      return; 
     } 
    }); 


    btn4.setOnClickListener(new Button.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      Intent myIntent = new Intent(); 
      myIntent = new Intent(getApplicationContext(), MainMisc.class); 
      myIntent.setAction(Intent.ACTION_VIEW); 
      myIntent.setData(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI); 
      setContentView(R.layout.misc_main); 
      return; 
     } 
    }); 

}}

답변

0

은 별도의 XML 레이아웃에서 버튼 모음을 정의합니다. 모든 XML에는 해당 막대가 <include/> 태그를 사용하는 단추 막대를 포함해야합니다. 그리고 XML의 모든 활동에서 버튼 막대를 포함 시키면 버튼의 onclick을 별도로 처리 할 수 ​​있습니다.

+0

고맙습니다. 모든 XML 레이아웃에 함수를 추가했습니다. 그것은 훨씬 더 효율적으로 내 코드를 만들었지 만 버튼을 클릭 할 때 다른 XML 레이아웃을 얻을 수있는 동일한 문제가 여전히 발생하지만 레이아웃에 영향을받습니다. – DuckiesUnite

+0

문제를 더 명확하게 설명해 주실 수 있습니까? 당신이 직면 한 정확한 문제는 무엇입니까? – stinepike

+0

레이아웃이 잘 나오고 모든 버튼을 클릭 할 수 있습니다. 예를 들어, btn3은 정확한 레이아웃으로 이동합니다.하지만 btn 1, btn2 및/또는 btn4를 클릭하면 btn3과 연결된 레이아웃이 유지됩니다. 나는 그것이 OnClick (View v)의 "return"과 관련이 있다고 생각했지만, 슬프게도 아무런 소용이 없었다. – DuckiesUnite

관련 문제