1.图片选择器
<?xml version="1.0" encoding="utf-8"?>
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_flower"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.mpyypm.flower.FloweerActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Please choose a flower you like!"
android:textSize="30dp"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:focusable="true"
android:focusableInTouchMode="true"
android:singleLine="true" />
<ImageView
android:id="@+id/img_flower"
android:layout_gravity="center_horizontal"
android:layout_width="350dp"
android:layout_height="200dp"
android:layout_weight="0.33"
android:src="@drawable/rose"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
<RadioGroup
android:id="@+id/flower1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rose"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="玫瑰"
android:textSize="25dp"/>
<RadioButton
android:id="@+id/xunyicao"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="薰衣草"
android:textSize="25dp" />
<RadioButton
android:id="@+id/yinghua"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="樱花"
android:textSize="25dp" />
</RadioGroup>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
<RadioGroup
android:id="@+id/flower2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<RadioButton
android:id="@+id/baihe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="百合"
android:textSize="25dp"
android:layout_weight="1" />
<RadioButton
android:id="@+id/youcaihua"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="油菜花"
android:textSize="25dp" />
<RadioButton
android:id="@+id/lanhua"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="兰花"
android:textSize="25dp" />
</LinearLayout>
</RadioGroup>
java代码
package com.example.mpyypm.flower;
import android.media.Image;
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 FloweerActivity extends AppCompatActivity {
private ImageView img_flower;
private RadioGroup flower1;
private RadioGroup flower2;
private RadioButton rose;
private RadioButton xunyicao;
private RadioButton yinghua;
private RadioButton baihe;
private RadioButton youcaihua;
private RadioButton lanhua;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_floweer);
img_flower = (ImageView) findViewById(R.id.img_flower);
flower1=(RadioGroup) findViewById(R.id.flower1);
flower2=(RadioGroup) findViewById(R.id.flower2);
rose=(RadioButton) findViewById(R.id.rose);
xunyicao=(RadioButton) findViewById(R.id.xunyicao);
yinghua=(RadioButton) findViewById(R.id.yinghua);
baihe=(RadioButton) findViewById(R.id.baihe);
youcaihua=(RadioButton) findViewById(R.id.youcaihua);
lanhua=(RadioButton) findViewById(R.id.lanhua);
rose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (rose.isChecked()){
img_flower.setImageResource(R.drawable.rose);
flower1();
}
}
});
baihe.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (baihe.isChecked()){
img_flower.setImageResource(R.drawable.baihe);
flower1();
}
}
});
yinghua.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (yinghua.isChecked()){
img_flower.setImageResource(R.drawable.yinghua);
flower1();
}
}
});
xunyicao.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (xunyicao.isChecked()){
img_flower.setImageResource(R.drawable.xunyicao);
flower2();
}
}
});
youcaihua.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (youcaihua.isChecked()){
img_flower.setImageResource(R.drawable.youcaihua);
flower2();
}
}
});
lanhua.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (lanhua.isChecked()){
img_flower.setImageResource(R.drawable.lanhua);
flower2();
}
}
});
}
void flower1(){
baihe.setChecked(false);
youcaihua.setChecked(false);
lanhua.setChecked(false);
}
void flower2(){
rose.setChecked(false);
xunyicao.setChecked(false);
yinghua.setChecked(false);
}
}
运行结果如下
***坐标
<?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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.mpyypm.xy.MainActivity">
<ImageView
android:layout_width="200dp"
android:layout_height="200dp"
android:src="@drawable/xq"
android:id="@+id/xq" />
</RelativeLayout>
java代码
package com.example.mpyypm.xy;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private ImageView xq;
private long time;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
xq = (ImageView) findViewById(R.id.xq);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction()==MotionEvent.ACTION_DOWN){
String pos= "";
float x= event.getX();
float y =event.getY();
xq.setPadding((int)x-100,(int)y-300,0,0);
pos="x轴坐标"+x+"y轴坐标"+y;
Toast.makeText(this,pos,Toast.LENGTH_SHORT).show();
}
return super.onTouchEvent(event);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getKeyCode()==KeyEvent.KEYCODE_BACK){
exit();
return false;
}
return super.onKeyDown(keyCode, event);
}
void exit(){
if(System.currentTimeMillis()-time>2000){
Toast.makeText(this,"确定退出!",Toast.LENGTH_SHORT).show();
time=System.currentTimeMillis();
}else {
finish();
}
}
}
运行结果
存在的问题
图片选择器中存在一点问题,有两种花,我选中了以后屏幕会显示这张图片,但是下面的按钮没有显示被选中,不知道问题在哪里。