设置按钮以引用多个xml布局

时间:2013-03-22 01:14:03

标签: android eclipse button

我有一个链接不同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 Intents更新)

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

}}

1 个答案:

答案 0 :(得分:0)

在单独的XML布局中定义按钮栏。在您需要的每个XML中,该栏包含使用<include/>标记的按钮栏。在包含XML的每个活动中,按钮栏分别处理按钮的单击。