xiaoxiao2022-06-29  35

    多界面初步框架

    第一个界面框架

    public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); new Thread(){ public void run() { SystemClock.sleep(4000); //停留四秒 // 跳转 Intent intent = new Intent(MainActivity.this,MainActivity2.class); startActivity(intent); finish(); //关闭自己,因为跳转到第二个页面后如果返回,不关闭第一个页面,页面还会存在 }; }.start(); } } 第二个界面

    /** * 输入页面 * @author admin * */ public class MainActivity2 extends Activity{ private static final String TAG = "MainActivity2"; private EditText filmname; private Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); filmname = (EditText) findViewById(R.id.et_name); button = (Button) findViewById(R.id.button); filmname.setText("猫咪"); //这里的内容定死了,其实应该在应用上输入的··· } public void search (View view){ String name = filmname.getText().toString(); //获取用户输入 if (TextUtils.isEmpty(name)) { Toast.makeText(MainActivity2.this, "请输入影视名", 0).show(); return; } byte[] by = name.getBytes(); //识别二进制,转换成数组 int number = 0; for (byte b : by) { number += Math.abs(b & 0xff); } Log.d(TAG, number/100+50+""); String str = ""; if (number>100) { str = "电影"; }else if (number>70) { str = "电视剧"; }else if (number>40) { str = "动漫"; } Intent intent = new Intent(MainActivity2.this,ThirdActivity.class); //跳转 intent.putExtra("搜索结果", str); //传递数据 startActivity(intent); } }

    第三个界面

    /** * 搜索结果界面 * @author admin */ public class ThirdActivity extends Activity{ private TextView show; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.item_picture); show = (TextView) findViewById(R.id.show); Intent intent = getIntent(); String result = intent.getStringExtra("搜索结果"); //接收数据 show.setText(result); } } 注意:两个Activity 都要在清单中注册,同一个包下面可以不用再写包名

    另外,其他两个Activity都要新建布局

    样式表

    根据需求,如果总是要改换布局,就可以在styles的样式表中设置布局(res-----value-----styles)

    <style name = "TextStyle"> <item name="android:textColor">#00bfff</item> <item name="android:textSize">25sp</item> <item name="android:background">#fffff0</item> <item name="android:layout_gravity">center_horizontal</item> </style> <style name="TextStyle2" parent="TextStyle"> <item name="android:textColor">#f08080</item> </style>

    界面之间的数据传递

    三种方式:

    1.serialized序列化传递(数据资源类接Serializable接口)

    传送方

    Intent intent = new Intent(MainActivity.this,SecondActivity.class); Computer computer = new Computer("花语月",16); intent.putExtra("computer", computer); startActivity(intent);

    接收方

    Computer compSeria = (Computer) intent.getSerializableExtra("computer"); text.setText(compSeria.toString());

    2.parcelable传递方式(数据资源类接Parcelable类,要覆写一些方法),同serialized

    3.bundle传递方式(数组传递时,没有putSerializable方法,因此接Serializable的类不能用bundle方式传递数组)

    传送方

    Bundle bundle = new Bundle(); Computer computer = new Computer("android studio",16); bundle.putSerializable("computer", computer); intent.putExtra("bundle", bundle);接收方

    Intent intent = getIntent(); //获取数据 Bundle bund = intent.getBundleExtra("bundle"); //获取bundle Computer compSeria = (Computer) bund.getSerializable("computer"); //强转类型

    Activity生命周期的6个过程

    public class MainActivity extends Activity { private static final String TAG = "MainActivity"; private EditText edit; private SharedPreferences shared; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Log.d(TAG, "onCreate"); shared = getSharedPreferences("config", 0); edit = (EditText) findViewById(R.id.et_text); } @Override protected void onStart() { super.onStart(); Log.d(TAG, "onStart"); } @Override protected void onResume() { super.onResume(); Log.d(TAG, "onResume"); //home键退出界面或者切换页面时,再回到页面数据依然保留(回显) edit.setText(shared.getString("data", " ")); } @Override protected void onPause() { super.onPause(); Log.d(TAG, "onPause"); } @Override protected void onStop() { super.onStop(); Log.d(TAG, "onStop"); } @Override protected void onDestroy() { super.onDestroy(); Log.d(TAG, "onDestroy"); //提交数据 shared.edit().putString("data", edit.getText().toString()).commit(); }

    转载请注明原文地址: https://ju.6miu.com/read-1125204.html

    最新回复(0)