3.14第四周android作业
1.综合使用TextView,ImageView,RadioButton控件实现一个图片选择器,通过勾选花朵的名称显示相应的图片,界面如附件图1、图2所示。具体要求如下:
- 使用滚动字幕显示标题“请选择你喜欢的花”;
- 使用RadioGroup 和RadioButton 创建两行三列的单选按钮;
- 当用户选中某一花名,在页面上显示该种花的图片;
滚动字幕,也就是我们说的TextView的跑马灯,在网上查阅了资料后有了如下答案:
<TextView
android:id="@+id/txt_light"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请选择你喜欢的相机品牌!"
android:textColor="@android:color/black"
android:ellipsize="marquee" //滚动
android:focusable="true" //获取焦点
android:focusableInTouchMode="true"
android:marqueeRepeatLimit="marquee_forever" // 滚动的时间是永远
android:singleLine="true" //单行显示
android:textSize="50dp"
/>
其中有几点需要注意的:
- 高度跟宽度不能设置成wrap_content,否则就会将全部内容都显示出来。
- .android:singleLine="true" 设置成单行模式
- Textview的获取焦点均设为true,才能管用。
···
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
>
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:orientation="horizontal"
android:gravity="left"
android:id="@+id/rgroup"
>
<RadioButton
android:id="@+id/bing_de"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="宾德"
android:textSize="20dp" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:text="康佳"
android:id="@+id/can_on"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:text="柯达"
android:id="@+id/ke_da"/>
</RadioGroup>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:id="@+id/rgroup2">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:text="卡西欧"
android:id="@+id/k_xo" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:text="尼康"
android:id="@+id/ni_kon" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:text="奥林巴斯"
android:id="@+id/aolinbasi"/>
</RadioGroup>
</LinearLayout>
···
因为要设置两行三列的按钮,所以我设置了两个
接下来是activity中的功能实现
public class MActivity extends AppCompatActivity {
private ImageView image_v;
private RadioGroup rgroup;
private RadioGroup rgroup2;
private RadioButton bing_de;
private RadioButton can_on;
private RadioButton ke_da;
private RadioButton k_xo;
private RadioButton ni_kon;
private RadioButton aolinbasi;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_m);
image_v=(ImageView)findViewById(R.id.image_v);//获取所有需要使用的控件对象
rgroup=(RadioGroup)findViewById(R.id.rgroup);
rgroup2=(RadioGroup)findViewById(R.id.rgroup2);
bing_de=(RadioButton)findViewById(R.id.bing_de);
can_on=(RadioButton)findViewById(R.id.can_on);
ke_da=(RadioButton)findViewById(R.id.ke_da);
k_xo=(RadioButton)findViewById(R.id.k_xo);
ni_kon=(RadioButton)findViewById(R.id.ni_kon);
aolinbasi=(RadioButton)findViewById(R.id.aolinbasi);
定义主键,并获取主键findViewById;
bing_de.setOnClickListener(new View.OnClickListener() {//设置监听按钮
@Override
public void onClick(View v) {
if (bing_de.isChecked()){
image_v.setImageResource(R.drawable.bingde);//选择显示的图片
camera1();//调用camera1方法
}
}
});
can_on.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (can_on.isChecked()){
image_v.setImageResource(R.drawable.canon);
camera1();
}
}
});
ke_da.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (ke_da.isChecked()){
image_v.setImageResource(R.drawable.keda);
camera1();
}
}
});
k_xo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (k_xo.isChecked()){
image_v.setImageResource(R.drawable.kxo);
camera2();
}
}
});
ni_kon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (ni_kon.isChecked()){
image_v.setImageResource(R.drawable.nikon);
camera2();
}
}
});
aolinbasi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (aolinbasi.isChecked()){
image_v.setImageResource(R.drawable.s);
camera2();
}
}
});
void camera1(){
k_xo.setChecked(false);
ni_kon.setChecked(false);
aolinbasi.setChecked(false);
}
void camera2(){
bing_de.setChecked(false);
can_on.setChecked(false);
ke_da.setChecked(false);
}
}
我设置了两个camera2方法因为有两个要实现只能选一个的目的所以我定义了两个camera2方法来使两排相互不能选择。
到此,任务一完成
2.完成如附件图3、图4所示的任务:
- 图片随着鼠标移动位置,并显示出当前位置的坐标信息。
- 当用户点击退出按钮,给出提示信息:“再按一次退出程序”。
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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.wdh.tap.MainActivity">
<ImageView
android:layout_width="99dp"
android:layout_height="91dp"
android:id="@+id/im"
android:src="@drawable/phone"
tools:layout_editor_absoluteY="166dp"
tools:layout_editor_absoluteX="134dp" />
</android.support.constraint.ConstraintLayout>
这是任务二的界面
public class MainActivity extends AppCompatActivity {
private ImageView im;
private long time;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
im=(ImageView)findViewById(R.id.im);//获取imageview控件的对象
}
@Override
public boolean onTouchEvent(MotionEvent event) {
//重写onTouchEvent方法
if (event.getAction() == MotionEvent.ACTION_DOWN) {
String zb = "";
float x = event.getX();//获取触碰位置的X坐标
float y = event.getY();//获取触碰位置的y坐标
im.setPadding((int)x-100,(int)y-300,0,0);
zb = "x轴坐标" + x + "y轴坐标" + y;
Toast.makeText(this, zb, 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();
}
}
}
这次用到了RadioButton,与其类似的还有CheckBox。所以我特地查了一下二者的差别
1.单个RadioButton在选中后,通过点击无法变为未选中,单个CheckBox在选中后,通过点击可以变为未选中
2.一组RadioButton,只能同时选中一个,一组CheckBox,能同时选中多个
3.RadioButton在大部分UI框架中默认都以圆形表示,CheckBox在大部分UI框架中默认都以矩形表示
4.大部分场合下,一个RadioGroup中至少有2个RadioButton
5.大部分场合下,一个RadioGroup中的RadioButton默认会有一个被选中,并建议您将它放在RadioGroup中的起始位置