1.创建安卓的项目
2.连接好自己的模拟器
3.使用直接拖动的方式实现布局
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:layout_width="fill_parent" 3 android:layout_height="fill_parent" 4 android:orientation="vertical" > 5 6 <LinearLayout 7 android:layout_width="match_parent" 8 android:layout_height="wrap_content" 9 android:orientation="vertical" > 10 11 <EditText 12 android:id="@+id/editText1" 13 android:layout_width="match_parent" 14 android:layout_height="wrap_content" 15 android:ems="10" 16 android:inputType="textPersonName" 17 android:text="请输入用户名" > 18 19 <requestFocus /> 20 </EditText> 21 22 <EditText 23 android:id="@+id/editText3" 24 android:layout_width="match_parent" 25 android:layout_height="wrap_content" 26 android:ems="10" 27 android:inputType="textPersonName" 28 android:text="请输入密码" /> 29 30 </LinearLayout> 31 32 <LinearLayout 33 android:layout_width="match_parent" 34 android:layout_height="wrap_content" 35 android:gravity="right" > 36 37 <Button 38 android:id="@+id/button1" 39 android:layout_width="wrap_content" 40 android:layout_height="wrap_content" 41 android:text="登录" /> 42 43 <Button 44 android:id="@+id/button2" 45 android:layout_width="wrap_content" 46 android:layout_height="wrap_content" 47 android:text="注册" /> 48 </LinearLayout> 49 50 <LinearLayout 51 android:layout_width="match_parent" 52 android:layout_height="wrap_content" > 53 54 <ImageView 55 android:id="@+id/imageView1" 56 android:layout_width="208dp" 57 android:layout_height="208dp" 58 android:layout_weight="0.52" 59 android:src="@drawable/ic_launcher" /> 60 61 </LinearLayout> 62 63 </LinearLayout>
4.编写MainActivity.java文件
1 package com.example.xzt; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.util.Log; 6 import android.view.KeyEvent; 7 import android.view.Menu; 8 import android.view.MenuItem; 9 import android.view.MotionEvent; 10 import android.view.View; 11 import android.view.View.OnClickListener; 12 import android.widget.Button; 13 import android.widget.ImageView; 14 import android.widget.TextView; 15 /** 16 * 该类实现的点击事件的接口 要重写点击的方法 17 * @author xingge 18 * 19 */ 20 21 public class MainActivity extends Activity implements OnClickListener { 22 /** 23 * 设置为全局变量后自动赋值为空 24 */ 25 ImageView image1; 26 int toumingdu=100; 27 private Button button1; 28 private Button button2; 29 TextView text1; 30 TextView text2; 31 @Override 32 protected void onCreate(Bundle savedInstanceState) { 33 super.onCreate(savedInstanceState); 34 setContentView(R.layout.activity_main); 35 /** 36 * 继承了Activity后 获取控件只要this点即可 37 */ 38 button1 = (Button) this.findViewById(R.id.button1); 39 button2 = (Button) this.findViewById(R.id.button2); 40 text1 =(TextView) this.findViewById(R.id.editText1); 41 text2 =(TextView) this.findViewById(R.id.editText3); 42 image1 = (ImageView) this.findViewById(R.id.imageView1); 43 44 button1.setOnClickListener(this); 45 button2.setOnClickListener(this); 46 } 47 48 @Override 49 public boolean onCreateOptionsMenu(Menu menu) { 50 // Inflate the menu; this adds items to the action bar if it is present. 51 getMenuInflater().inflate(R.menu.main, menu); 52 return true; 53 } 54 55 @Override 56 public boolean onOptionsItemSelected(MenuItem item) { 57 // Handle action bar item clicks here. The action bar will 58 // automatically handle clicks on the Home/Up button, so long 59 // as you specify a parent activity in AndroidManifest.xml. 60 int id = item.getItemId(); 61 if (id == R.id.action_settings) { 62 return true; 63 } 64 return super.onOptionsItemSelected(item); 65 } 66 67 @Override 68 public void onClick(View arg0) { 69 /** 70 * argo 表示获取点击事件是空间的id 71 */ 72 switch (arg0.getId()) { 73 case R.id.button1: 74 text1.setText("login"); 75 break; 76 case R.id.button2: 77 text2.setText("logout"); 78 break; 79 80 81 } 82 83 84 85 } 86 87 @Override 88 public boolean onKeyDown(int keyCode, KeyEvent event) { 89 // TODO Auto-generated method stub 90 switch (keyCode) { 91 case KeyEvent.KEYCODE_DPAD_UP: 92 toumingdu +=10; 93 image1.setAlpha(toumingdu);//设置透明度的方法 94 break; 95 case KeyEvent.KEYCODE_DPAD_DOWN: 96 toumingdu -=10; 97 image1.setAlpha(toumingdu);//设置透明度的方法 98 break; 99 100 101 } 102 103 return super.onKeyDown(keyCode, event); 104 } 105 106 /** 107 * 显示触摸的位置 108 */ 109 @Override 110 public boolean onTouchEvent(MotionEvent event) { 111 // TODO Auto-generated method stub 112 float x = event.getX();//获得x坐标 113 float y = event.getY(); 114 text1.setText("位置x:"+x);//设置文本 115 text2.setText("位置y:"+y); 116 117 return super.onTouchEvent(event); 118 } 119 120 121 }
5.运行结果