0

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

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

}}

4

1 回答 1

0

Define your button bar in a separate XML layout. In every XML you need that bar include that button bar using <include/> tag. And in every activity in which XML you have included the button bar handle the onclick of the buttons separately.

于 2013-03-22T01:18:03.830 回答