结对编程
文档说明:
这个demo实现的功能是用户登陆后点击球员的名字可以显示图片
关键代码
1 public class MainActivity extends AppCompatActivity { 2 EditText et_name; 3 EditText et_password; 4 Button bt_login; 5 6 @Override 7 protected void onCreate(Bundle savedInstanceState) { 8 super.onCreate(savedInstanceState); 9 setContentView(R.layout.activity_main); 10 et_name =(EditText)findViewById(R.id.et_name); 11 et_password=(EditText)findViewById(R.id.et_passwaord); 12 bt_login =(Button)findViewById(R.id.bt_login); 13 bt_login.setOnClickListener(new View.OnClickListener() { 14 @Override 15 public void onClick(View view) { 16 String name =et_name.getText().toString(); 17 String password =et_password.getText().toString(); 18 if(TextUtils.isEmpty(name)&&TextUtils.isEmpty(password)){ 19 Toast.makeText(MainActivity.this,"请输入用户名和密码",Toast.LENGTH_SHORT).show(); 20 21 }else if(TextUtils.isEmpty(name)&&password!=null){ 22 Toast.makeText(MainActivity.this,"请输入用户名",Toast.LENGTH_SHORT).show(); 23 24 25 }else if(TextUtils.isEmpty(password)&&name!=null){ 26 Toast.makeText(MainActivity.this,"请输入密码",Toast.LENGTH_SHORT).show(); 27 28 }else{ 29 if(name.equals("niit")&&password.equals("123456")) { 30 Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show(); 31 Intent myIntent = new Intent(MainActivity.this, UserActivity.class); 32 startActivity(myIntent); 33 }else{ 34 Toast.makeText(MainActivity.this, "登录失败", Toast.LENGTH_SHORT).show(); 35 } 36 } 37 } 38 39 }); 40 } 41 }
1 public class UserActivity extends AppCompatActivity { 2 Button kb; 3 Button back_m; 4 @Override 5 protected void onCreate(@Nullable Bundle savedInstanceState) { 6 super.onCreate(savedInstanceState); 7 setContentView(R.layout.userlayout); 8 kb=(Button) findViewById(R.id.kb); 9 back_m=(Button)findViewById(R.id.back_m) ; 10 kb.setOnClickListener(new View.OnClickListener() { 11 @Override 12 public void onClick(View view) { 13 Intent kb = new Intent(UserActivity.this,KobeAcicity_two.class); 14 startActivity(kb); 15 } 16 }); 17 back_m.setOnClickListener(new View.OnClickListener() { 18 @Override 19 public void onClick(View v) { 20 Intent back_m = new Intent(UserActivity.this,MainActivity.class); 21 startActivity(back_m); 22 } 23 }); 24 } 25 }
1 public class KobeAcicity_two extends Activity { 2 private Button btn; 3 private ImageView img; 4 private String url = "https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=4267085115,1950863501&fm=23&gp=0.jpg"; 5 6 7 private Handler handle = new Handler() { 8 public void handleMessage(Message msg) { 9 switch (msg.what) { 10 case 0: 11 System.out.println("111"); 12 Bitmap bmp=(Bitmap)msg.obj; 13 img.setImageBitmap(bmp); 14 break; 15 } 16 }; 17 }; 18 @Override 19 protected void onCreate(Bundle savedInstanceState) { 20 super.onCreate(savedInstanceState); 21 setContentView(R.layout.kobelayout); 22 23 btn = (Button) findViewById(R.id.ld); 24 img = (ImageView) findViewById(R.id.kobe); 25 26 btn.setOnClickListener(new View.OnClickListener() { 27 28 @Override 29 public void onClick(View v) { 30 31 new Thread(new Runnable() { 32 33 @Override 34 public void run() { 35 // TODO Auto-generated method stub 36 Bitmap bmp = getURLimage(url); 37 Message msg = new Message(); 38 msg.what = 0; 39 msg.obj = bmp; 40 System.out.println("000"); 41 handle.sendMessage(msg); 42 } 43 }).start(); 44 } 45 }); 46 } 47 48 49 public Bitmap getURLimage(String url) { 50 Bitmap bmp = null; 51 try { 52 URL myurl = new URL(url); 53 54 HttpURLConnection conn = (HttpURLConnection) myurl.openConnection(); 55 conn.setConnectTimeout(10000); 56 conn.setDoInput(true); 57 conn.setUseCaches(false); 58 conn.connect(); 59 InputStream is = conn.getInputStream(); 60 bmp = BitmapFactory.decodeStream(is); 61 is.close(); 62 } catch (Exception e) { 63 e.printStackTrace(); 64 } 65 return bmp; 66 } 67 }