안드로이드 개발(4) 등록 인터페이스

80396 단어
1. 등록 인터페이스는 새 사용자가 등록하는 데 사용된다.사용자는 간단한 정보만 입력하면 새로운 사용자가 될 수 있고 다른 정보는 나중에 보충할 수 있다.그 중에서 사용자가 사용자 이름과 비밀번호를 입력해야 하고 자신의 이미지를 사용자 정의할 수 있다.
2. 인터페이스 레이아웃 파일 activityregister.xml:

   
   
   
   
1 < TableLayout xmlns:android ="http://schemas.android.com/apk/res/android" 2 xmlns:tools ="http://schemas.android.com/tools" 3 android:layout_width ="fill_parent" 4 android:layout_height ="fill_parent" 5 android:orientation ="vertical" 6 > 7 < TableRow 8 android:layout_width ="match_parent" 9 android:layout_height ="wrap_content" > 10 < ImageView 11 android:id ="@+id/img_show" 12 android:layout_width ="wrap_content" 13 android:layout_height ="wrap_content" 14 android:layout_span ="2" 15 android:src ="@drawable/header" 16 android:maxWidth ="30dp" 17 android:maxHeight ="30dp" 18 /> 19 </ TableRow > 20 < TableRow > 21 < TextView 22 android:layout_width ="fill_parent" 23 android:layout_height ="wrap_content" 24 android:text ="@string/account" 25 android:textSize ="10pt" 26 /> 27 28 < EditText 29 android:id ="@+id/user" 30 android:layout_width ="fill_parent" 31 android:layout_height ="wrap_content" 32 android:ems ="10" 33 android:hint ="@string/message" 34 android:selectAllOnFocus ="true" > 35 36 </ EditText > 37 38 </ TableRow > 39 < TableRow > 40 < TextView 41 android:layout_width ="fill_parent" 42 android:layout_height ="wrap_content" 43 android:text ="@string/password" 44 android:textSize ="10pt" 45 /> 46 < EditText 47 android:id ="@+id/password" 48 android:layout_width ="fill_parent" 49 android:layout_height ="wrap_content" 50 android:inputType ="textPassword" 51 /> 52 </ TableRow > 53 < TableRow > 54 < TextView 55 android:layout_width ="fill_parent" 56 android:layout_height ="wrap_content" 57 android:text ="@string/confirmpassword" 58 android:textSize ="10pt" 59 /> 60 < EditText 61 android:id ="@+id/confirmpassword" 62 android:layout_width ="fill_parent" 63 android:layout_height ="wrap_content" 64 android:inputType ="textPassword" 65 /> 66 </ TableRow > 67 < TableRow > 68 < TextView 69 android:layout_width ="fill_parent" 70 android:layout_height ="wrap_content" 71 android:text ="@string/sex" 72 android:textSize ="10pt" 73 /> 74 < RadioGroup 75 android:id ="@+id/radioGroup" 76 android:orientation ="horizontal" 77 android:layout_gravity ="center_horizontal" > 78 < RadioButton 79 android:id ="@+id/male" 80 android:layout_width ="wrap_content" 81 android:layout_height ="wrap_content" 82 android:text ="@string/male" /> 83 < RadioButton 84 android:id ="@+id/female" 85 android:layout_width ="wrap_content" 86 android:layout_height ="wrap_content" 87 android:text ="@string/female" /> 88 </ RadioGroup > 89 </ TableRow > 90 < TableRow 91 android:layout_width ="match_parent" 92 android:layout_height ="wrap_content" > 93 < Button 94 android:id ="@+id/uploadIcon" 95 android:layout_width ="wrap_content" 96 android:layout_height ="wrap_content" 97 android:layout_span ="2" 98 android:text ="@string/uploadIcon" /> 99 100 </ TableRow > 101 < TableRow 102 android:layout_width ="match_parent" 103 android:layout_height ="wrap_content" > 104 < Button 105 android:id ="@+id/ok" 106 android:layout_width ="wrap_content" 107 android:layout_height ="wrap_content" 108 android:layout_span ="2" 109 android:text ="@string/ok" /> 110 111 </ TableRow > 112 </ TableLayout >

레이아웃 파일은 테이블과 같은 TableLayout을 사용합니다.위쪽에 이미지를 표시하고 가운데에 배치합니다.layout_span = "2", 두 열에 걸쳐 있습니다.
3. 새 Android Activity를 만들어 비즈니스 작업을 수행합니다.Eclipse를 사용하여 새 File->New->Other->Android->Android Activity를 만들 수 있습니다.
SelectFileActivity: 업로드 단추를 누르면 SelectFileActivity가 파일을 선택합니다.

   
   
   
   
1 package com.diandianmi.activity; 2 3 import java.io.File; 4 import java.util.ArrayList; 5 6 import android.os.Bundle; 7 import android.os.Environment; 8 import android.app.Activity; 9 import android.content.Intent; 10 import android.view.KeyEvent; 11 import android.view.Menu; 12 import android.view.View; 13 import android.widget.AdapterView; 14 import android.widget.ArrayAdapter; 15 import android.widget.ListView; 16 import android.widget.AdapterView.OnItemClickListener; 17 18 public class SelectFileActivity extends Activity { 19 20 private ListView lvShow; 21 22 private ArrayList < File > fileList = new ArrayList < File > (); // list 23 private ArrayList < String > fileNames = new ArrayList < String > ();; // 24 private String currentFilePath = null ; 25 26 public static String FILEPATH = " filepath " ; 27 28 @Override 29 protected void onCreate(Bundle savedInstanceState) { 30 super .onCreate(savedInstanceState); 31 setContentView(R.layout.activity_select_file); 32 lvShow = (ListView) this .findViewById(R.id.lv_select); 33 34 lvShow.setOnItemClickListener( new OnItemClickListener() { 35 36 @Override 37 public void onItemClick(AdapterView <?> adapter, View v, 38 int postion, long id) { 39 40 if (fileList.get(postion).isDirectory()) { 41 loadFile(fileList.get(postion)); 42 return ; 43 } 44 45 Intent intent = new Intent(); 46 intent.putExtra(FILEPATH, fileList.get(postion).getAbsolutePath()); // / , 。 47 setResult(RegisterActivity.IMAGE_SELECT, intent); 48 finish(); 49 } 50 }); 51 52 loadFile(Environment.getExternalStorageDirectory()); 53 } 54 55 private ArrayAdapter < String > setAdapter() { 56 ArrayAdapter < String > adapter = new ArrayAdapter < String > ( 57 SelectFileActivity. this , android.R.layout.simple_list_item_1,fileNames); 58 59 return adapter; 60 } 61 /** 62 * 63 * 64 * @param file 65 */ 66 private void loadFile(File file) { 67 68 // list 69 if (fileList.size() > 0 ) { 70 fileList.clear(); 71 } 72 if (fileNames.size() > 0 ) { 73 fileNames.clear(); 74 } 75 // load 76 File[] files = file.listFiles(); 77 // 78 if (files == null ) { 79 return ; 80 } 81 82 for ( int i = 0 ; i < files.length; i ++ ) { 83 fileList.add(files[i]); 84 fileNames.add(files[i].getName()); 85 } 86 87 currentFilePath = file.getAbsolutePath(); 88 lvShow.setAdapter(setAdapter()); 89 }; 90 @Override 91 public boolean onKeyDown( int keyCode, KeyEvent event) { 92 93 if (keyCode == KeyEvent.KEYCODE_BACK) { // 94 if (currentFilePath.equals( " / " )) { 95 finish(); 96 } else { 97 loadFile( new File(currentFilePath).getParentFile()); 98 } 99 } 100 101 return super .onKeyDown(keyCode, event); 102 } 103 104 @Override 105 public boolean onCreateOptionsMenu(Menu menu) { 106 // Inflate the menu; this adds items to the action bar if it is present. 107 getMenuInflater().inflate(R.menu.select_file, menu); 108 return true ; 109 } 110 111 } 112

선택한 그림의 경로를 RegisterActivity에 전송하여 편집을 하고, 다른 것은 안드로이드에서 제공하는 편집 기능을 사용합니다.
RegisterActivity:

   
   
   
   
1 package com.diandianmi.activity; 2 3 import java.io.File; 4 import java.io.IOException; 5 import java.text.SimpleDateFormat; 6 import java.util.Date; 7 8 import com.diandianmi.model.UserInfo; 9 import com.diandianmi.service.DataBaseService; 10 import android.net.Uri; 11 import android.os.Bundle; 12 import android.annotation.SuppressLint; 13 import android.app.Activity; 14 import android.content.Intent; 15 import android.content.SharedPreferences; 16 import android.graphics.Bitmap; 17 import android.graphics.BitmapFactory; 18 import android.view.KeyEvent; 19 import android.view.Menu; 20 import android.view.View; 21 import android.view.View.OnClickListener; 22 import android.widget.Button; 23 import android.widget.EditText; 24 import android.widget.ImageView; 25 import android.widget.RadioGroup; 26 import android.widget.RadioGroup.OnCheckedChangeListener; 27 import android.widget.TextView; 28 import android.widget.TextView.OnEditorActionListener; 29 import android.widget.Toast; 30 31 public class RegisterActivity extends Activity { 32 33 private EditText name = null ; 34 35 private ImageView imgShow; 36 // private Bitmap mBitmap; 37 private String imgPath = null ; 38 // private String username; 39 /** 40 * Bitmap 41 * */ 42 private Bitmap mTmpBmp; 43 44 private Boolean isHeader = false ; 45 46 private static final int IMAGE_CUT = 1 ; 47 public static final int IMAGE_SELECT = 2 ; 48 49 private SharedPreferences preferences; 50 private SharedPreferences.Editor editor; 51 52 // private DataBaseService dbSer = null; 53 private UserInfo user = null ; 54 @SuppressLint({ " CommitPrefEdits " , " SimpleDateFormat " }) 55 @Override 56 protected void onCreate(Bundle savedInstanceState) { 57 super .onCreate(savedInstanceState); 58 setContentView(R.layout.activity_register); 59 // dbSer = new DataBaseService(RegisterActivity.this); 60 imgShow = (ImageView) this .findViewById(R.id.img_show); 61 imgShow.setAdjustViewBounds( true ); // 62 63 preferences = getSharedPreferences( " diandianmi " ,MODE_PRIVATE); 64 editor = preferences.edit(); 65 66 SimpleDateFormat sdf = new SimpleDateFormat( " yyyy-MM-dd hh:mm:ss " ); 67 editor.putString( " time " , sdf.format( new Date())); 68 name = (EditText)findViewById(R.id.user); 69 final EditText password = (EditText)findViewById(R.id.password); 70 final EditText confirmpassword = (EditText)findViewById(R.id.confirmpassword); 71 RadioGroup group = (RadioGroup)findViewById(R.id.radioGroup); 72 73 Button uploadBtn = (Button)findViewById(R.id.uploadIcon); 74 Button ok = (Button)findViewById(R.id.ok); 75 76 user = new UserInfo(); 77 78 79 confirmpassword.setOnEditorActionListener( new OnEditorActionListener(){ // 80 81 @Override 82 public boolean onEditorAction(TextView v, int actionId, 83 KeyEvent event) { 84 // TODO Auto-generated method stub 85 if ( ! password.getText().toString().equals(confirmpassword.getText().toString())){ 86 Toast.makeText(RegisterActivity. this , " the password is wrong " , Toast.LENGTH_SHORT).show(); 87 } 88 89 return false ; 90 } 91 92 }); 93 94 group.setOnCheckedChangeListener( new OnCheckedChangeListener(){ // 95 96 @Override 97 public void onCheckedChanged(RadioGroup arg0, int arg1) { 98 // TODO Auto-generated method stub 99 if (arg1 == R.id.male){ 100 user.setSex( " male " ); 101 editor.putString( " sex " , " male " ); 102 } else if (arg1 == R.id.female){ 103 user.setSex( " female " ); 104 editor.putString( " sex " , " female " ); 105 } 106 } 107 108 }); 109 110 uploadBtn.setOnClickListener( new OnClickListener(){ // 111 112 @Override 113 public void onClick(View v) { 114 // TODO Auto-generated method stub 115 Intent intent = new Intent(RegisterActivity. this ,SelectFileActivity. class ); // Activity, SD 116 startActivityForResult(intent, IMAGE_SELECT); 117 } 118 119 }); 120 121 ok.setOnClickListener( new OnClickListener(){ 122 123 @Override 124 public void onClick(View arg0) { 125 // TODO Auto-generated method stub 126 user.setName(name.getText().toString()); 127 editor.putString( " username " , name.getText().toString()); 128 user.setPassword(password.getText().toString()); 129 if ( ! password.getText().toString().equals(confirmpassword.getText().toString())){ 130 Toast.makeText(RegisterActivity. this , " the password is wrong " , Toast.LENGTH_SHORT).show(); 131 return ; 132 } 133 DataBaseService dbSer = new DataBaseService(RegisterActivity. this ); 134 135 if ( ! isHeader){ 136 Bitmap rawBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.header); // 137 user.setIcon(rawBitmap); 138 } 139 140 dbSer.addUser(user); // 141 142 dbSer.close(); 143 144 Intent intent = new Intent(RegisterActivity. this ,LoginActivity. class ); // 145 startActivity(intent); 146 } 147 148 }); 149 } 150 151 /** 152 * android 153 */ 154 private void runIntent(Uri uri) { 155 156 System.out.println( " Uri " + uri); 157 158 Intent intent = new Intent( " com.android.camera.action.CROP " ); 159 intent.setDataAndType(uri, " image/* " ); 160 intent.putExtra( " crop " , " true " ); 161 intent.putExtra( " aspectX " , 1 ); 162 intent.putExtra( " aspectY " , 1 ); 163 intent.putExtra( " outputX " , 80 ); 164 intent.putExtra( " outputY " , 80 ); 165 intent.putExtra( " return-data " , true ); 166 167 startActivityForResult(intent, IMAGE_CUT); 168 } 169 170 /** 171 * sd 172 * @throws IOException 173 */ 174 /* 175 private void saveImage() throws IOException { 176 177 178 //dbSer.updateUserIcon(mTmpBmp,name.getText().toString() ); 179 180 FileService fileSer = new FileService(); 181 String imgSavePath = fileSer.getSDcard() ; 182 183 File dirFile = new File(imgSavePath); 184 if (!dirFile.exists()) { 185 dirFile.mkdirs(); 186 } 187 188 FileOutputStream fos = null; 189 try { 190 fos = new FileOutputStream(imgSavePath + System.currentTimeMillis() 191 + ".jpg"); 192 mTmpBmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);// JPEG 193 } catch (FileNotFoundException e) { 194 e.printStackTrace(); 195 } finally { 196 try { 197 fos.flush(); 198 fos.close(); 199 } catch (IOException e) { 200 e.printStackTrace(); 201 } 202 } 203 } */ 204 205 206 @Override 207 protected void onActivityResult( int requestCode, int resultCode, Intent data) { 208 // TODO Auto-generated method stub 209 super .onActivityResult(requestCode, resultCode, data); 210 if (requestCode == IMAGE_CUT && data != null ) { // 211 mTmpBmp = data.getParcelableExtra( " data " ); // 212 // saveImage(); 213 user.setIcon(mTmpBmp); 214 isHeader = true ; 215 216 imgShow.setImageBitmap(mTmpBmp); 217 218 } else if (requestCode == IMAGE_SELECT && data != null ) { // SelectFileActivity 219 imgPath = data.getStringExtra(SelectFileActivity.FILEPATH); // 220 File file = new File(imgPath); 221 Uri uri = Uri.fromFile(file); 222 runIntent(uri); // 223 224 } else { 225 imgShow.setImageBitmap(mTmpBmp); 226 } 227 // btnCut.setVisibility(View.VISIBLE); 228 } 229 230 @Override 231 public boolean onCreateOptionsMenu(Menu menu) { 232 // Inflate the menu; this adds items to the action bar if it is present. 233 getMenuInflater().inflate(R.menu.register, menu); 234 return true ; 235 } 236 237 } 238

등록 인터페이스가 완료되었습니다.여기서 잘라내기 인터페이스는 Android에서 사용할 수 있습니다.

좋은 웹페이지 즐겨찾기