<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:weightSum="1" tools:context="com.example.yz.yz.MainActivity"> <ImageView android:id="@+id/iv_image" android:layout_width="80dp" android:layout_height="80dp" android:src="@drawable/xg"/> </LinearLayout> package com.example.yz.yz; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.MotionEvent; import android.view.View; import android.widget.ImageView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private ImageView iv_image; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); iv_image = (ImageView) findViewById(R.id.iv_image); iv_image.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this,"再点一次退出?",Toast.LENGTH_SHORT).show(); } }); } @Override public boolean onTouchEvent(MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { float x = event.getX(); float y = event.getY(); iv_image.setPadding((int) x - 10, (int) y - 30, 20, 20); Toast.makeText(this, x + " " + y, Toast.LENGTH_SHORT).show(); } return super.onTouchEvent(event); } } <?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:orientation="vertical" android:weightSum="1"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="50dp" android:singleLine="true" android:focusableInTouchMode="true" android:focusable="true" android:ellipsize="marquee" android:gravity="center" android:textColor="@android:color/holo_red_dark" android:text="Please choose a flower you like!" /> <ImageView android:layout_gravity="center_horizontal" android:layout_width="225dp" android:layout_height="150dp" android:id="@+id/iv_iamge" android:layout_weight="0.27" /> <RadioGroup android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_gravity="center" > <RadioButton android:id="@+id/rb_mh" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@android:color/holo_red_dark" android:text="梅花 " android:textSize="20dp" android:textColorLink="@android:color/holo_red_dark" /> <RadioButton android:id="@+id/rb_snh" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@android:color/holo_red_dark" android:text="石楠花" android:textSize="20dp" android:textColorHint="@android:color/darker_gray" /> <RadioButton android:id="@+id/rb_xyh" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@android:color/holo_red_dark" android:text="象牙花" android:textSize="20dp" /> </RadioGroup> <RadioGroup android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_gravity="center" > <RadioButton android:id="@+id/rb_ylh" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@android:color/holo_red_dark" android:text="玉兰花" android:textSize="20dp" /> <RadioButton android:id="@+id/rb_mdh" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@android:color/holo_red_dark" android:text="牡丹花" android:textSize="20dp" /> <RadioButton android:id="@+id/rb_xqh" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@android:color/holo_red_dark" android:text="绣球花" android:textSize="20dp" /> </RadioGroup> </LinearLayout> <?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:orientation="vertical" android:weightSum="1"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="50dp" android:singleLine="true" android:focusableInTouchMode="true" android:focusable="true" android:ellipsize="marquee" android:gravity="center" android:textColor="@android:color/holo_red_dark" android:text="Please choose a flower you like!" /> <ImageView android:layout_gravity="center_horizontal" android:layout_width="225dp" android:layout_height="150dp" android:id="@+id/iv_iamge" android:layout_weight="0.27" /> <RadioGroup android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_gravity="center" > <RadioButton android:id="@+id/rb_mh" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@android:color/holo_red_dark" android:text="梅花 " android:textSize="20dp" android:textColorLink="@android:color/holo_red_dark" /> <RadioButton android:id="@+id/rb_snh" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@android:color/holo_red_dark" android:text="石楠花" android:textSize="20dp" android:textColorHint="@android:color/darker_gray" /> <RadioButton android:id="@+id/rb_xyh" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@android:color/holo_red_dark" android:text="象牙花" android:textSize="20dp" /> </RadioGroup> <RadioGroup android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_gravity="center" > <RadioButton android:id="@+id/rb_ylh" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@android:color/holo_red_dark" android:text="玉兰花" android:textSize="20dp" /> <RadioButton android:id="@+id/rb_mdh" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@android:color/holo_red_dark" android:text="牡丹花" android:textSize="20dp" /> <RadioButton android:id="@+id/rb_xqh" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@android:color/holo_red_dark" android:text="绣球花" android:textSize="20dp" /> </RadioGroup> </LinearLayout>
package com.example.yz.myhome; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.ImageView; import android.widget.RadioButton; import android.widget.RadioGroup; public class MainActivity extends AppCompatActivity { private ImageView iv_iamge; private RadioButton rb_mh; private RadioButton rb_snh; private RadioButton rb_xyh; private RadioButton rb_xqh; private RadioButton rb_mdh; private RadioButton rb_ylh; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); iv_iamge=(ImageView) findViewById(R.id. iv_iamge); rb_mh=(RadioButton) findViewById(R.id.rb_mh); rb_snh=(RadioButton) findViewById(R.id.rb_snh); rb_xyh=(RadioButton) findViewById(R.id.rb_xyh); rb_xqh=(RadioButton) findViewById(R.id.rb_xqh); rb_mdh=(RadioButton) findViewById(R.id.rb_mdh); rb_ylh=(RadioButton)findViewById(R.id.rb_ylh) ; rb_mh.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v){ group2(); iv_iamge.setImageResource(R.drawable.c); } }); rb_snh.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { group2(); iv_iamge.setImageResource(R.drawable.c); } }); rb_xqh.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { group1(); iv_iamge.setImageResource(R.drawable.d); } }); rb_mdh.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { group1(); iv_iamge.setImageResource(R.drawable.d); } }); rb_xyh.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { group2(); iv_iamge.setImageResource(R.drawable.f); } }); rb_ylh.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { group1(); iv_iamge.setImageResource(R.drawable.a); } }); } void group1(){ rb_mh.setChecked(false); rb_xyh.setChecked(false); rb_snh.setChecked(false); } void group2(){ rb_ylh.setChecked(false); rb_xqh.setChecked(false); rb_mdh.setChecked(false); }