안드로이드 - 다중 페이지 개발

73358 단어
1. 리스트 파일 소개
1.1 응용 프로그램에 여러 개의 시작 입구를 설정하려면 다음과 같이 설정해야 한다.
 
   
  1. <activity android:name=".Main2Activity">
  2. <intent-filter>
  3. <action android:name="android.intent.action.MAIN" />
  4. <category android:name="android.intent.category.LAUNCHER" />
  5. intent-filter>
  6. activity>
 1.2 application节点下的icon和label可以和activity节点下不一样 如果activity自己配置了icon和label属性使用自己的.

2.意图

   1.显示意图:通过设置具体的包名类名开启页面
   2.隐式意图:通过设置一组动作或者数据开启页面
   3.思考什么时候用显示 什么时候用隐式:开系统应用的页面用隐式 自己应用页面用显示

3.人品计算器

   功能:计算人品 
   代码实现过程:
   1.实现UI
    
   2.mainActivity代码如下:
 
   
  1. public class MainActivity extends AppCompatActivity {
  2. private EditText et_name;
  3. private RadioGroup rg_group;
  4. @Override
  5. protected void onCreate(Bundle savedInstanceState) {
  6. super.onCreate(savedInstanceState);
  7. setContentView(R.layout.activity_main);
  8. //1.
  9. et_name = (EditText) findViewById(R.id.et_name);
  10. rg_group = (RadioGroup) findViewById(R.id.rg_group);
  11. }
  12. //
  13. public void click(View view) {
  14. //1.
  15. String name = et_name.getText().toString().trim();
  16. //2. name
  17. if (TextUtils.isEmpty(name)){
  18. Toast.makeText(this, "name ", Toast.LENGTH_SHORT).show();
  19. return;
  20. }
  21. //3.
  22. int sex = 0;
  23. int checkedRadioButtonId = rg_group.getCheckedRadioButtonId();
  24. switch (checkedRadioButtonId){
  25. case R.id.rb_male: //
  26. sex = 1;
  27. break;
  28. case R.id.rb_female: //
  29. sex = 2;
  30. break;
  31. case R.id.rb_other: //
  32. sex = 3;
  33. break;
  34. }
  35. //4.
  36. if (sex == 0){
  37. Toast.makeText(this, " ", Toast.LENGTH_SHORT).show();
  38. return;
  39. }
  40. //5.
  41. Intent intent = new Intent(this, ResultActivity.class);
  42. //6. map( : )
  43. intent.putExtra("name",name);
  44. intent.putExtra("sex",sex);
  45. //7.
  46. startActivity(intent);
  47. }
  48. }
 3. :
 
   
  1. public class ResultActivity extends AppCompatActivity {
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.activity_result);
  6. //1.
  7. TextView tv_name = (TextView) findViewById(R.id.tv_name);
  8. TextView tv_sex = (TextView) findViewById(R.id.tv_sex);
  9. TextView tv_result = (TextView) findViewById(R.id.tv_result);
  10. //2.
  11. Intent intent = getIntent(); //
  12. String name = intent.getStringExtra("name");
  13. int sex = intent.getIntExtra("sex", 0);
  14. //3.
  15. tv_name.setText(name);
  16. switch (sex){
  17. case 1: //
  18. tv_sex.setText(" ");
  19. break;
  20. case 2: //
  21. tv_sex.setText(" ");
  22. break;
  23. case 3: //
  24. tv_sex.setText(" ");
  25. break;
  26. }
  27. //4.
  28. Random random = new Random();
  29. int score = random.nextInt(101);
  30. if (score> 90){
  31. tv_result.setText(" ");
  32. }else if (score > 75){
  33. tv_result.setText(" , ");
  34. }else if (score > 60){
  35. tv_result.setText(" ");
  36. }else{
  37. tv_result.setText(" 。 ?");
  38. }
  39. }
  40. }

4.

 
   
  1. public class MainActivity extends AppCompatActivity {
  2. String[] objects = { " , ; , ; , 。 ..."
  3. ," , , , 。 , , 。 , , ..."
  4. ," , ; , 。 , , , ..."
  5. ," , : , ; , ; , ; ..."};
  6. @Override
  7. protected void onCreate(Bundle savedInstanceState) {
  8. super.onCreate(savedInstanceState);
  9. setContentView(R.layout.activity_main);
  10. //1, listview
  11. ListView lv = (ListView) findViewById(R.id.lv);
  12. //2. listview
  13. lv.setAdapter(new ArrayAdapter<String>(getApplicationContext(),R.layout.item,R.id.tv,objects));
  14. //3. listview button:
  15. lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  16. // list
  17. @Override
  18. public void onItemClick(AdapterView> parent, View view, int position, long id) {
  19. //4.
  20. String data = objects[position];
  21. //5.
  22. Intent intent = new Intent();
  23. //5.1
  24. intent.setAction("android.intent.action.SEND");
  25. intent.addCategory("android.intent.category.DEFAULT");
  26. intent.setType("text/plain");
  27. //5.2 data
  28. intent.putExtra("sms_body",data);
  29. //6.
  30. startActivity(intent);
  31. }
  32. });
  33. }

5.

   2 .3 : 2   A B   A B
  2 ,A B , B , B A 
   
   1. UI
    
      2.mainActivity
 
   
  1. public class MainActivity extends AppCompatActivity {
  2. private EditText et_context;
  3. private EditText et_number;
  4. @Override
  5. protected void onCreate(Bundle savedInstanceState) {
  6. super.onCreate(savedInstanceState);
  7. setContentView(R.layout.activity_main);
  8. //1.
  9. et_context = (EditText) findViewById(R.id.et_content);
  10. et_number = (EditText) findViewById(R.id.et_number);
  11. }
  12. // ContactActivity
  13. public void add(View view) {
  14. Intent intent = new Intent(this, ContactActivity.class);
  15. // startActivity(intent);
  16. //
  17. startActivityForResult(intent,1);
  18. }
  19. //
  20. public void click(View view) {
  21. Intent intent = new Intent(this, SmsTemplateActivity.class);
  22. startActivityForResult(intent,2);
  23. }
  24. //
  25. public void send(View view) {
  26. //1.
  27. String content = et_context.getText().toString().trim();
  28. String number = et_number.getText().toString().trim();
  29. //2. smsmanager
  30. SmsManager smsmanager = SmsManager.getDefault();
  31. //3.
  32. ArrayList<String> smss = smsmanager.divideMessage(content);
  33. for (String sms : smss) {
  34. //4.
  35. smsmanager.sendTextMessage(number,null,sms,null,null);
  36. }
  37. }
  38. // finish
  39. @Override
  40. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  41. if (resultCode == 10){
  42. //
  43. //1.
  44. String phone = data.getStringExtra("phone");
  45. //2. phone EditText
  46. et_number.setText(phone);
  47. }else if (resultCode == 20){
  48. //1.
  49. String smsContent = data.getStringExtra("smsContent");
  50. //2. smsContent EditText
  51. et_context.setText(smsContent);
  52. }
  53. }
  54. }
 3. :
 
   
  1. package com.itheima.smssend;
  2. import android.content.Intent;
  3. import android.support.v7.app.AppCompatActivity;
  4. import android.os.Bundle;
  5. import android.text.format.Formatter;
  6. import android.view.View;
  7. import android.view.ViewGroup;
  8. import android.widget.AdapterView;
  9. import android.widget.BaseAdapter;
  10. import android.widget.ListView;
  11. import android.widget.TextView;
  12. import org.w3c.dom.Text;
  13. import java.util.ArrayList;
  14. import java.util.List;
  15. public class ContactActivity extends AppCompatActivity {
  16. private List<Person> lists;
  17. @Override
  18. protected void onCreate(Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.activity_contact);
  21. //1. listview
  22. ListView lv_contact = (ListView) findViewById(R.id.lv_contact);
  23. //2. ( )
  24. lists = new ArrayList<>();
  25. for (int i = 0; i <= 10; i++) {
  26. Person person = new Person();
  27. person.setName("zhangsan"+i);
  28. person.setPhone("110"+i);
  29. lists.add(person);
  30. }
  31. //3. listview
  32. lv_contact.setAdapter(new MyAdapter());
  33. //4. listview
  34. lv_contact.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  35. @Override
  36. public void onItemClick(AdapterView> parent, View view, int position, long id) {
  37. //5.
  38. String phone = lists.get(position).getPhone();
  39. //5.1
  40. Intent intent = new Intent();
  41. intent.putExtra("phone",phone);
  42. //5.2
  43. setResult(10,intent);
  44. //6.
  45. finish(); // onActivityResult
  46. }
  47. });
  48. }
  49. // listview
  50. class MyAdapter extends BaseAdapter{
  51. @Override
  52. public int getCount() {
  53. return lists.size();
  54. }
  55. @Override
  56. public Object getItem(int position) {
  57. return null;
  58. }
  59. @Override
  60. public long getItemId(int position) {
  61. return 0;
  62. }
  63. @Override
  64. public View getView(int position, View convertView, ViewGroup parent) {
  65. //0. listview
  66. View view;
  67. if (convertView == null){
  68. view = View.inflate(getApplicationContext(),R.layout.item,null);
  69. }else{
  70. view = convertView;
  71. }
  72. //1.
  73. TextView tv_name = (TextView) view.findViewById(R.id.tv_name);
  74. TextView tv_phone = (TextView) view.findViewById(R.id.tv_phone);
  75. //2.
  76. tv_name.setText(lists.get(position).getName());
  77. tv_phone.setText(lists.get(position).getPhone());
  78. return view;
  79. }
  80. }
  81. }
4.
 
   
  1. public class SmsTemplateActivity extends AppCompatActivity {
  2. String objects[] = { " , "," , "," , "," , "};
  3. @Override
  4. protected void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.activity_sms_template);
  7. //1.
  8. ListView listView = (ListView) findViewById(R.id.lv);
  9. listView.setAdapter(new ArrayAdapter<String>(getApplicationContext(),R.layout.smstemplate_item,R.id.textView,objects));
  10. //2. listview
  11. listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  12. @Override
  13. public void onItemClick(AdapterView> parent, View view, int position, long id) {
  14. //3.
  15. String smsContent = objects[position];
  16. //4.
  17. Intent intent = new Intent();
  18. intent.putExtra("smsContent",smsContent);
  19. //5.
  20. setResult(20,intent);
  21. //6. finsih
  22. finish();
  23. }
  24. });
  25. }
  26. }

6.Activity  

   1.onCreate activity
   2.onDestory activity
   3.onStart activity ( )
   4.onStop activity
   5.onResume ( ) 
   6.onPause  
   7.onRestart  


   
   
  
  
       
   

좋은 웹페이지 즐겨찾기