3.17作业
3.17作业
选择你喜欢的花
1. 布局设计代码
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Please choose a flower you like!"
android:textSize="40dp"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:focusable="true"
android:focusableInTouchMode="true"
android:singleLine="true" />
首先设计了TextView,在经过网上查找以及自我思考后,给标题文字设了“跑马灯”的滚动状态,并且设置永远跑马灯;
标题文字要设置成单行显示也就是singleLine,以及文字要比TextView长;
要在文字上获得焦点;
<ImageView
android:id="@+id/img_flower"
android:layout_gravity="center_horizontal"
android:layout_width="350dp"
android:layout_height="200dp"
android:layout_weight="0.33"/>
设置显示图片的框;
<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/rbt_lihua"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="梨花 "
android:textSize="25dp"/>
<RadioButton
android:id="@+id/rbt_meihua"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="梅花 "
android:textSize="25dp" />
<RadioButton
android:id="@+id/rbt_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/rbt_shuixianhua"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="水仙花"
android:textSize="25dp"/>
<RadioButton
android:id="@+id/rbt_youcaihua"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="油菜花"
android:textSize="25dp" />
<RadioButton
android:id="@+id/rbt_yulanhua"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="玉兰花"
android:textSize="25dp" />
</RadioGroup>
</LinearLayout>
设置六个按钮;
因为要两排三列 所以我用了两个LinearLayout布局;
2.运行代码
public class MainActivity extends AppCompatActivity {
private ImageView img_flower;
private RadioGroup flower1;
private RadioGroup flower2;
private RadioButton rbt_lihua;
private RadioButton rbt_meihua;
private RadioButton rbt_yinghua;
private RadioButton rbt_youcaihua;
private RadioButton rbt_shuixianhua;
private RadioButton rbt_yulanhua;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img_flower = (ImageView) findViewById(R.id.img_flower);
flower1=(RadioGroup) findViewById(R.id.flower1);
flower2=(RadioGroup) findViewById(R.id.flower2);
rbt_lihua=(RadioButton) findViewById(R.id.rbt_lihua);
rbt_meihua=(RadioButton) findViewById(R.id.rbt_meihua);
rbt_yinghua=(RadioButton) findViewById(R.id.rbt_yinghua);
rbt_youcaihua=(RadioButton) findViewById(R.id.rbt_youcaihua);
rbt_shuixianhua=(RadioButton) findViewById(R.id.rbt_shuixianhua);
rbt_yulanhua=(RadioButton) findViewById(R.id.rbt_yulanhua);
定义主键;
获取主键 findViewById;
rbt_lihua.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (rbt_lihua.isChecked()){
img_flower.setImageResource(R.drawable.lihua);
no1();
}
}
});
rbt_meihua.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (rbt_meihua.isChecked()){
img_flower.setImageResource(R.drawable.meihua);
no1();
}
}
});
rbt_yinghua.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (rbt_yinghua.isChecked()){
img_flower.setImageResource(R.drawable.yinghua);
no1();
}
}
});
rbt_shuixianhua.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (rbt_shuixianhua.isChecked()){
img_flower.setImageResource(R.drawable.shuixianhua);
no2();
}
}
});
rbt_youcaihua.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (rbt_youcaihua.isChecked()){
img_flower.setImageResource(R.drawable.youcaihua);
no2();
}
}
});
rbt_yulanhua.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (rbt_yulanhua.isChecked()){
img_flower.setImageResource(R.drawable.yulanhua);
no2();
}
}
});
}
设置监听按钮;
通过RadioButton的选中状态获取
void no1(){
rbt_shuixianhua.setChecked(false);
rbt_youcaihua.setChecked(false);
rbt_yulanhua.setChecked(false);
}
void no2(){
rbt_meihua.setChecked(false);
rbt_lihua.setChecked(false);
rbt_yinghua.setChecked(false);
}
}
因为设置了两组RadioGroup,所以,当点第一组的RadioButton时第二组也能被选上,所以这里要设置无返回值类型,同时上面也要调用这个方法;
坐标信息
1.布局设计代码
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/b"/>
很简单的布局代码,只需要插入一张图就好了
2.运行代码
public class MainActivity extends AppCompatActivity {
private ImageView img;
private long time;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = (ImageView) findViewById(R.id.img);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction()==MotionEvent.ACTION_DOWN){
String pos= "";
float x= event.getX();
float y =event.getY();
img.setPadding((int)x-50,(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();
}
}
}