安卓课后作业

2017.3.19

任务一

  1. 综合使用TextView,ImageView,RadioButton控件实现一个图片选择器,通过勾选花朵的名称显示相应的图片,界面如附件图1、图2所示。具体要求如下:
    a) 使用滚动字幕显示标题“请选择你喜欢的花”
    b) 使用RadioGroup 和RadioButton 创建两行三列的单选按钮;
    c) 当用户选中某一花名,在页面上显示该种花的图片

布局界面

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@android:color/holo_blue_dark"
    android:layout_gravity="center"
    android:ellipsize="marquee"-跑马灯左右滚动
    android:focusable="true"-允许该button获取焦点
    android:focusableInTouchMode="true"-设置焦点联系方式(正确的)
    android:marqueeRepeatLimit="marquee_forever"-设置跑马灯一直滚动
    android:scrollHorizontally="true"
    android:scrollbars="horizontal"
    android:singleLine="true"
    android:text="Please choose a flower you like"
    android:textSize="30dp"
    android:id="@+id/biaoti" /> 

通过跑马灯来实现字幕的左右滚动

<ImageView
    android:layout_marginTop="30dp"
    android:layout_width="200dp"
    android:layout_height="140dp"
    android:layout_gravity="center"
    android:id="@+id/tupian"/>

<RadioGroup
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:orientation="vertical"
    android:id="@+id/hualei"
    >

    <RadioGroup
        android:layout_marginTop="40dp"
        android:id="@+id/rg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal"
        >

        <RadioButton

            android:id="@+id/meihua"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="@dimen/activity_vertical_margin"
            android:checked="true"
            android:text="梅花"
            android:textSize="20sp"
            />

        <RadioButton

            android:id="@+id/shinanhua"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="石楠花   "
            android:textSize="20sp"/>
        <RadioButton

            android:id="@+id/xiangyahua"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="象牙花"
            android:textSize="20sp"/>
    </RadioGroup>

    <RadioGroup

    android:id="@+id/rg2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="horizontal"
    >

    <RadioButton

        android:id="@+id/xiuqiuhua"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="绣球花"
        android:textSize="20sp"
        />

    <RadioButton

        android:id="@+id/shilanhua"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="石兰花   "
        android:textSize="20sp"/>

    <RadioButton

        android:id="@+id/mudanhua"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="牡丹花"
        android:textSize="20sp"/>
</RadioGroup>
</RadioGroup>
通过创建了2个RadioGroup组rgrg和rgrg2用于监听 ![](http://images2015.cnblogs.com/blog/1126498/201703/1126498-20170319104610073-1887481773.png) ###java语言编辑 public class MainActivity extends AppCompatActivity { private TextView txbiaoti; private RadioButton rbmeihua; private RadioButton rbshinanhua; private RadioButton rbxiangyahua; private RadioButton rbxiuqiuhua; private RadioButton rbshilanhua; private RadioButton rbmudanhua; private RadioGroup rgrg; private RadioGroup rgrg2; private RadioGroup rghualei; private ImageView ivtupian;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    txbiaoti = (TextView) findViewById(R.id.biaoti);
    rbmeihua = (RadioButton) findViewById(R.id.meihua);
    rbshinanhua = (RadioButton) findViewById(R.id.shinanhua);
    rbxiangyahua = (RadioButton) findViewById(R.id.xiangyahua);
    rbxiuqiuhua = (RadioButton) findViewById(R.id.xiuqiuhua);
    rbshilanhua = (RadioButton) findViewById(R.id.shilanhua);
    rbmudanhua = (RadioButton) findViewById(R.id.mudanhua);
    rgrg = (RadioGroup) findViewById(R.id.rg);
    rgrg2 = (RadioGroup) findViewById(R.id.rg2);
    rghualei = (RadioGroup) findViewById(R.id.hualei);
    ivtupian=(ImageView)findViewById(R.id.tupian);//建立相关的事件响应

    rgrg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {   //监听rgrg RadioGroup组
        @Override
        public void onCheckedChanged(RadioGroup radioGroup, int i) {
        if(rbmeihua.isChecked()){
            ivtupian.setImageResource(R.drawable.meihua);//设置ImageView ivtupina的图片属性,将自己想要的图片植入,下面的同样道理
            rgrg2.clearCheck();//清除rgrg2 RadioGroup组中的选择,用于显示单选
        } 
            if(rbshinanhua.isChecked()){
                ivtupian.setImageResource(R.drawable.shinanhua);
                rgrg2.clearCheck();
            }
            if(rbxiangyahua.isChecked()){
                ivtupian.setImageResource(R.drawable.iangyahua);
                rgrg2.clearCheck();
            }

        }
    });
    rgrg2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {//监听rgrg2 RadioGroup组
        @Override
        public void onCheckedChanged(RadioGroup radioGroup, int i) {
            if(rbxiuqiuhua.isChecked()){
                ivtupian.setImageResource(R.drawable.xiuqiuhua);
                rgrg.clearCheck();
            }
            if(rbshilanhua.isChecked()){
                ivtupian.setImageResource(R.drawable.shilanhua);
                rgrg.clearCheck();
            }
            if(rbmudanhua.isChecked()){
                ivtupian.setImageResource(R.drawable.mudanhua);
                rgrg.clearCheck();
            }

        }
    });

}

}

成品展示

任务二

  1. 完成如附件图3、图4所示的任务:
    a) 图片随着鼠标移动位置,并显示出当前位置的坐标信息。
    b) 当用户点击退出按钮,给出提示信息:“再按一次退出程序”。

布局界面

<ImageView
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:layout_gravity="center_horizontal"
    android:id="@+id/tupian" />

<Button
    android:layout_marginTop="300dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:text="退出"
    android:id="@+id/button" />
###Java编辑界面 public class MainActivity extends AppCompatActivity { private Button btbutton; private ImageView ivtupian;
//按钮退出代码
@Override
protected void onCreate(Bundle savedInstanceState) { //监听按钮
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btbutton = (Button) findViewById(R.id.button);
    ivtupian = (ImageView) findViewById(R.id.tupian);
    btbutton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (btbutton.isClickable()) {
                Toast.makeText(MainActivity.this, "再按一次退出按钮",
                        Toast.LENGTH_LONG).show();//如果退出按钮被点击一下,会显示文本再按一次退出按钮
                btbutton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (btbutton.isClickable()) {
                            MainActivity.this.finish();//再一次监听按钮,如果再按一次退出按钮,就会关闭整个界面
                        }
                    }
                });
            }
        }
    });
}

//获取坐标代码
public boolean onTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_MOVE) {
        float x = event.getX();
        float y = event.getY();
        String pos = "x坐标" + x + "y坐标" + y;//获取鼠标的x,y坐标值
        Toast.makeText(this, pos, Toast.LENGTH_LONG).show();//通过Toast文本方法实现输出坐标值
        ivtupian.setImageResource(R.drawable.renwu);//给ImageView添加图片
        ivtupian.setX(x - 50);//赋值给图片的X坐标
        ivtupian.setY(y - 160);//赋值给图片的Y坐标,为了好看,x,y都减了一定的值,使得鼠标基本在图片中心点上
    }
    return super.onTouchEvent(event);
}

//手机上退出键代码
private long exitTime = 0;

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {//判断手机上退出键
        if ((System.currentTimeMillis() - exitTime) > 2000) {
            Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
            exitTime = System.currentTimeMillis();
        } else {
            finish();
            System.exit(0);
        }
        return true;
    }
    return super.onKeyDown(keyCode, event);
}//参考于百度http://www.cnblogs.com/jxgxy/archive/2012/08/23/2653404.html

}

成品展示


posted on 2017-03-19 11:59  丁金涛  阅读(288)  评论(1)    收藏  举报

导航