3.17作业
3.17作業
選擇你喜歡的花
1. 布局設計代碼
<TextViewandroid: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長;
要在文字上獲得焦點;
<ImageViewandroid:id="@+id/img_flower"android:layout_gravity="center_horizontal"android:layout_width="350dp"android:layout_height="200dp"android:layout_weight="0.33"/>設置顯示圖片的框;
<LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"><RadioGroupandroid:id="@+id/flower1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:gravity="center"android:orientation="horizontal"><RadioButtonandroid:id="@+id/rbt_lihua"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="梨花 "android:textSize="25dp"/><RadioButtonandroid:id="@+id/rbt_meihua"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="梅花 "android:textSize="25dp" /><RadioButtonandroid:id="@+id/rbt_yinghua"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="櫻花 "android:textSize="25dp" /></RadioGroup></LinearLayout> <LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"><RadioGroupandroid:id="@+id/flower2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:gravity="center"android:orientation="horizontal"><RadioButtonandroid:id="@+id/rbt_shuixianhua"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="水仙花"android:textSize="25dp"/><RadioButtonandroid:id="@+id/rbt_youcaihua"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="油菜花"android:textSize="25dp" /><RadioButtonandroid: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() {@Overridepublic void onClick(View view) {if (rbt_lihua.isChecked()){img_flower.setImageResource(R.drawable.lihua);no1();}}});rbt_meihua.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {if (rbt_meihua.isChecked()){img_flower.setImageResource(R.drawable.meihua);no1();}}});rbt_yinghua.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {if (rbt_yinghua.isChecked()){img_flower.setImageResource(R.drawable.yinghua);no1();}}});rbt_shuixianhua.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {if (rbt_shuixianhua.isChecked()){img_flower.setImageResource(R.drawable.shuixianhua);no2();}}});rbt_youcaihua.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {if (rbt_youcaihua.isChecked()){img_flower.setImageResource(R.drawable.youcaihua);no2();}}});rbt_yulanhua.setOnClickListener(new View.OnClickListener() {@Overridepublic 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.布局設計代碼
<ImageViewandroid: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();} } }轉載于:https://www.cnblogs.com/yeech/p/6582945.html
總結
- 上一篇: C++实现树的基本操作,界面友好,操作方
- 下一篇: 完全理解Android中的RemoteV