在Switch(v.getId())中获取图像视图

时间:2016-03-01 17:57:48

标签: android android-studio imageview switch-statement android-imageview

我正在尝试进行练习,这意味着我需要在使用imageView作为按钮时从imageView更改图像,然后单击它。但是我在设置switch case语句时遇到错误

import android.app.Activity;
import android.os.Bundle;
import android.content.pm.ActivityInfo;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.Switch;


public class MainActivity extends Activity implements OnClickListener {
     ImageView display;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    display = (ImageView) findViewById(R.id.imageView);
    ImageView image1 = (ImageView) findViewById(R.id.iVmg1);
    ImageView image2 = (ImageView) findViewById(R.id.iVmg2);
    ImageView image3 = (ImageView) findViewById(R.id.iVmg3);
    ImageView image4 = (ImageView) findViewById(R.id.iVmg4);
    ImageView image5 = (ImageView) findViewById(R.id.iVmg5);
    ImageView image6 = (ImageView) findViewById(R.id.iVmg6);
    ImageView image7 = (ImageView) findViewById(R.id.iVmg7);
    ImageView image8 = (ImageView) findViewById(R.id.iVmg8);
    ImageView image9 = (ImageView) findViewById(R.id.iVmg9);
    ImageView image10 = (ImageView) findViewById(R.id.iVmg10);



    image1.setOnClickListener(this);
    image2.setOnClickListener(this);
    image3.setOnClickListener(this);
    image4.setOnClickListener(this);
    image5.setOnClickListener(this);
    image6.setOnClickListener(this);
    image7.setOnClickListener(this);
    image8.setOnClickListener(this);
    image9.setOnClickListener(this);
    image10.setOnClickListener(this);

}


@Override
public void onClick(View v) {
     Switch(v.getId()) {
            case R.id.iVmg1:
                display.setImageResource(R.drawable.img1);
                break;
            case R.id.iVmg2:
                display.setImageResource(R.drawable.img2);
                break;
            case R.id.iVmg3:
                display.setImageResource(R.drawable.img3);
                break;
            case R.id.iVmg4:
                display.setImageResource(R.drawable.img4);
                break;
            case R.id.iVmg5:
                display.setImageResource(R.drawable.img5);
                break;
            case R.id.iVmg6:
                display.setImageResource(R.drawable.img6);
                break;
            case R.id.iVmg7:
                display.setImageResource(R.drawable.img7);
                break;
            case R.id.iVmg8:
                display.setImageResource(R.drawable.img8);
                break;
            case R.id.iVmg9:
                display.setImageResource(R.drawable.img9);
                break;
            case R.id.iVmg10:
                display.setImageResource(R.drawable.img10);
                break;


    }
}
}

XML代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res     /android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.neelaysrivastava.wallpaper.MainActivity"
tools:showIn="@layout/activity_main"
android:background="#000000">

<ImageView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/imageView"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:src="@drawable/img1" />
<HorizontalScrollView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/horizontalScrollView"
    android:layout_below="@+id/button"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    >
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <ImageView
            android:layout_width="125dp"
            android:layout_height="wrap_content"
            android:src="@drawable/img1"
            android:id="@+id/iVmg1"
            />
        <ImageView
            android:layout_width="125dp"
            android:layout_height="wrap_content"
            android:src="@drawable/img2"
            android:id="@+id/iVmg2"
            />
        <ImageView
            android:layout_width="125dp"
            android:layout_height="wrap_content"
            android:src="@drawable/img3"
            android:id="@+id/iVmg3"
            />
        <ImageView
            android:layout_width="125dp"
            android:layout_height="wrap_content"
            android:src="@drawable/img4"
            android:id="@+id/iVmg4"
            />
        <ImageView
            android:layout_width="125dp"
            android:layout_height="wrap_content"
            android:src="@drawable/img5"
            android:id="@+id/iVmg5"
            />
        <ImageView
            android:layout_width="125dp"
            android:layout_height="wrap_content"
            android:src="@drawable/img6"
            android:id="@+id/iVmg6"
            />
        <ImageView
            android:layout_width="125dp"
            android:layout_height="wrap_content"
            android:src="@drawable/img7"
            android:id="@+id/iVmg7"
            />
        <ImageView
            android:layout_width="125dp"
            android:layout_height="wrap_content"
            android:src="@drawable/img8"
            android:id="@+id/iVmg8"
            />
        <ImageView
            android:layout_width="125dp"
            android:layout_height="wrap_content"
            android:src="@drawable/img9"
            android:id="@+id/iVmg9"
            />
        <ImageView
            android:layout_width="125dp"
            android:layout_height="wrap_content"
            android:src="@drawable/img10"
            android:id="@+id/iVmg10"
            />
 </LinearLayout>
</HorizontalScrollView>
</RelativeLayout>

错误:错误:任务&#39;:app:compileDebugJavaWithJavac&#39;。

执行失败
  

编译失败;请参阅编译器错误输出以获取详细信   错误:(67,27)错误:&#39;;&#39;预期   错误:(68,17)错误:孤立的情况   F:\ SOFTWARE \壁纸\应用\ SRC \主\的java \ COM \示例\ neelaysrivastava \壁纸\ MainActivity.java

1 个答案:

答案 0 :(得分:0)

替换

img=Image.open('cat.png')
img = img.convert("L")
pix=img.load()

Switch(v.getId())

这只是switch语句的编译错误。

并且switch(v.getId()) 错误意味着您需要包含orphaned case部分:

default