0

今天早上刚开始学习Android,需要帮助。我的应用程序中有几个按钮,我希望当用户单击按钮时将显示图像并返回按钮以加载 main.xml 。

代码:

在 main.xml

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

    <Button 
        android:layout_marginTop="40dp"
           android:layout_width="100dp"
           android:layout_height="80dp"
           android:id="@+id/b1"
           android:text="xyz"
           android:background="#ff3375"
           android:layout_marginLeft="20dp"
        />

    <Button 
        android:layout_marginTop="40dp"
           android:layout_width="100dp"
           android:layout_height="80dp"
           android:id="@+id/b2"
           android:layout_toRightOf="@id/b1"
           android:text="abc"
           android:background="#ff3375"
           android:layout_marginLeft="80dp"
        /></RelativeLayout>

而在 Activity.java

package com.sam;

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

public class A2Activity extends Activity {
    /** Called when the activity is first created. */

    Button a,b;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        a= (Button) findViewById(R.id.b1);
        b= (Button) findViewById(R.id.b2);
        a.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub

        }
    });
        b.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub

        }
    });}}

现在,我需要在 onCLick 方法中添加什么来打开图像或 XML 文件以及返回到 main.xml 的后退按钮

4

1 回答 1

1

请进一步解释您的问题。你是什​​么意思打开图像或xml文件?你的意思是创建一个位图?你的意思是展示它吗?如果有,在哪里?另外,你的后退按钮将返回到 main.xml 是什么意思?main.xml 是一个布局文件,而不是一个活动。

无论如何,也许您的意思是您希望在按下按钮时全屏打开图像,然后单击设备的后退按钮返回您创建的活动?

如果是这样,您可以创建一个扩展 Activity 的新类,更新清单以使其可访问,然后启动它(使用 startActivity)。在新的活动类中,将内容视图设置为显示图像的 ImageView 或具有显示图像的 imageView 的布局文件。


要开始一项新活动,您需要调用:

startActivity(new Intent(CurrentActivity.this, NewActivity.class);

其中“CurrentActivity”是您所在的当前活动(在您的示例中称为“A2Activity”),“NewActivity”是显示图像的活动。

如前所述,不要忘记更新清单。

于 2012-06-13T22:07:23.310 回答