使用按钮

时间:2015-05-07 06:35:17

标签: android imageview

大家好,很抱歉,如果这个问题得到解答。

我正在做一个地理游戏,我想知道如何根据我按下的按钮更改我的ImageView。

例如,我有一张带箭头的欧洲地图指向西班牙。我希望当我按下西班牙按钮时,地图会变成带箭头的地图,指向葡萄牙。

我已经尝试过这段代码,但我无法使其正常工作:

    public void cargar_por(View v){
    ImageView iv = (ImageView)findViewById(R.id.imageView);
    if(flag==false){
        Drawable por=setFeatureDrawable(iv,por);
        iv.setImageDrawable(R.drawable.por);
        flag = true;
    }
    else{
        flag=false;
    }
}

我怎么能这样做?

提前致谢。

编辑:我要添加完整的代码。

xml文件如下所示:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">

<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:weightSum="1">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_weight="0.91"
        android:weightSum="1"
        android:focusableInTouchMode="true"
        android:focusable="true">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal">

            <Chronometer
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/chronometer"
                android:layout_gravity="right"
                android:enabled="true" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:text="Nivel 1"
                android:id="@+id/textView5"
                android:layout_gravity="center_horizontal"
                android:background="#ffffffff" />
        </LinearLayout>

        <TableLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/map"
                android:src="@drawable/spa"
                android:visibility="visible"
                android:layout_weight="0.93" />

        </TableLayout>

    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="131dp"
        android:layout_gravity="center_horizontal"
        android:background="#3f073dff">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/layout1">

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="fill_parent"
                android:layout_height="60dp"
                android:weightSum="1">

                <LinearLayout
                    android:orientation="horizontal"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:weightSum="1">

                    <Button
                        android:layout_width="181dp"
                        android:layout_height="match_parent"
                        android:text="ESPAÑA"
                        android:id="@+id/button5"
                        android:onClick="cargar_por" />
                </LinearLayout>

                <LinearLayout
                    android:orientation="horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <Button
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:text="PORTUGAL"
                        android:id="@+id/button6" />
                </LinearLayout>
            </LinearLayout>

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center_horizontal"
                android:weightSum="1">

                <LinearLayout
                    android:orientation="horizontal"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_weight="0.40"
                    android:weightSum="1">

                    <Button
                        android:layout_width="match_parent"
                        android:layout_height="58dp"
                        android:text="FRANCIA"
                        android:id="@+id/button7"
                        android:layout_weight="0.86" />
                </LinearLayout>

                <LinearLayout
                    android:orientation="horizontal"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_weight="0.40">

                    <Button
                        android:layout_width="match_parent"
                        android:layout_height="59dp"
                        android:text="ITALIA"
                        android:id="@+id/button8" />
                </LinearLayout>

            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

这样的Java代码:

package marti.victor.geografia;

import android.graphics.drawable.Drawable;
import android.os.SystemClock;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.*;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.ImageButton;
import android.widget.ImageView;


public class Juego extends ActionBarActivity {
//    boolean flag = false;
    ImageView image = (ImageView) findViewById(R.id.map);
    Button but;
    int a = 0;
    Chronometer myChronometer;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.juego);
    myChronometer = (Chronometer)findViewById(R.id.chronometer);
    myChronometer.start();



}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main_activity2, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}



/*public void cargar_por(View v){
    ImageView iv = (ImageView)findViewById(R.id.imageView);
    but=(Button)findViewById(R.id.button5);
    but.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            iv.setImageDrawable(R.drawable.por);
        }
    });
}*/
}

1 个答案:

答案 0 :(得分:1)

试试这个

  ImageView iv = (ImageView)findViewById(R.id.imageView);
    spainbutton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                   iv.setImageDrawable(R.drawable.por);     
                 }
            });

在你的情况下,我认为旗帜不能正常工作。