ANDROID:如何一次只选择一个ImageView?

时间:2016-09-09 11:36:45

标签: android imageview

我将四个ImageView设置为菜单。 在四个菜单项中,我想一次只选择一个。

我为一个菜单项设置了2个图像,如选择1,另外3个取消选择。

这里我发布了代码:

footer.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:background="#f1eeee"
    android:orientation="horizontal">
<ImageView
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:id="@+id/fHome"
    android:src="@drawable/home" />  <!-- your image here -->

<ImageView
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:id="@+id/fAttendence"
    android:src="@drawable/home" />

<ImageView
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:id="@+id/home"
    android:src="@drawable/target" />
<ImageView
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:id="@+id/home"
    android:src="@drawable/report" />

main_activity.xml

   <?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context="com.example.sachin.colorchanged.MainActivity">
<include
    layout="@layout/footer"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:layout_alignParentBottom="true"
    android:id="@+id/include2" />
 </RelativeLayout>

Mainactivity

public class MainActivity extends AppCompatActivity {
    ImageView mHomeButton,mattendence,mTarget,mReport;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mHomeButton = (ImageView) findViewById(R.id.fHome);
        mattendence = (ImageView) findViewById(R.id.fAttendence);
        mTarget= (ImageView) findViewById(R.id.fTarget);
        mReport = (ImageView) findViewById(R.id.fReport);

        mHomeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //Set the button's appearance
                mHomeButton.setSelected(!mHomeButton.isSelected());
                if (mHomeButton.isSelected()) {
                    //Handle selected state change
                    mHomeButton.setImageResource(R.drawable.blue);
                } else {
                    //Handle de-select state change
                    mHomeButton.setImageResource(R.drawable.home);
                }

            }
        });
        mattendence.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mattendence.setSelected(!mattendence.isSelected());
                if (mattendence.isSelected()) {
                    //Handle selected state change
                    mattendence.setImageResource(R.drawable.blue);
                } else {
                    //Handle de-select state change
                    mattendence.setImageResource(R.drawable.home);
                }
            }
        });
        mTarget.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mTarget.setSelected(!mTarget.isSelected());
                if (mTarget.isSelected()) {
                    //Handle selected state change
                    mTarget.setImageResource(R.drawable.blue);
                } else {
                    //Handle de-select state change
                    mTarget.setImageResource(R.drawable.home);
                }
            }
        });
        mReport.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mReport.setSelected(!mReport.isSelected());
                if (mReport.isSelected()) {
                    //Handle selected state change
                    mReport.setImageResource(R.drawable.blue);
                } else {
                    //Handle de-select state change
                    mReport.setImageResource(R.drawable.home);
                }
            }
        });

    }
}

[home.png for deselected item]

1 [blue.png for selected item] 2

请帮我解决这个问题并向我推荐这个例子..

1 个答案:

答案 0 :(得分:2)

首先让您的班级实现View.OnClickListener

public class MainActivity extends AppCompatActivity implements View.OnClickListener 

然后实现接口的onClick方法:

@Override
public void OnClick(View v)

现在你OnClick方法首先加载图像的所有默认图像,如:

  mHomeButton.setImageResource(R.drawable.home);
  mattendence.setImageResource(R.drawable.home); // and other imageViews

然后在onClick方法中将switch个案定义为:

switch (v.getId()){
  case R.id.fHome:
     mHomeButton.setImageResource(R.drawable.blue);
     break;
  // define same as all other views
}

最终代码为:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    ImageView mHomeButton,mattendence,mTarget,mReport;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mHomeButton = (ImageView) findViewById(R.id.fHome);
        mattendence = (ImageView) findViewById(R.id.fAttendence);
        mTarget= (ImageView) findViewById(R.id.fTarget);
        mReport = (ImageView) findViewById(R.id.fReport);

       mHomeButton.setOnClickListener(this);
       mattendence.setOnClickListener(this);
       mTarget.setOnClickListener(this);
       mReport.setOnClickListener(this);
    }

    @Override
    public void OnClick(View v){
        mHomeButton.setImageResource(R.drawable.home);
        mattendence.setImageResource(R.drawable.home);
        mTarget.setImageResource(R.drawable.home);
        mReport.setImageResource(R.drawable.home);
        int selected = 0;
        Intent intent = new Intent(this,YOURACTIVITY.class);


        switch(v.getId()){

        case R.id.fHome:
            mHomeButton.setImageResource(R.drawable.blue);
            selected = 1;
            break;

        case R.id.mTarget:
            mTarget.setImageResource(R.drawable.blue);
            selected = 2;
            break;

        case R.id.fAttendence:
            mattendence.setImageResource(R.drawable.blue);
            selected = 3;
            break;


        case R.id.mReport:
            mReport.setImageResource(R.drawable.blue);
             selected = 4;
            break;


      } 
      intent.putExtra("selected",selected);
      startActivity(intent);  
}

然后在您的其他活动中,您必须检查选择了哪个imageView:

int selected = getIntent().getIntExtra("selected",0);
   switch(int){
   case 1:
     mHomeButton.setImageResource(R.drawable.blue);
     break;
   case 2 ... and so on
    }