Android开发笔记(三)之新增页面跳转和数据传递,系统设置界面

本次笔记将记录在项目中增添页面转换及Intent和Bundle方法的数据传递,新增系统设置界面,并使用preference方法存储新增页面输入的内容。

先创建Book类,使之可序列化,进行Bundle方式的数据传递,代码:

import java.io.Serializable;

public class Book implements Serializable {
    public String bid;
    public String bname;
    public String author;
    public String local;
}

设置新增图书页面的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    <TextView
        android:id="@+id/menu"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="新增图书"
        android:textSize="35dp"
        android:textColor="#000"
        android:background="#6CF">
    </TextView>
    <RelativeLayout
        android:id="@+id/Btn1"
        android:layout_below="@id/menu"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dip"
        android:layout_centerHorizontal="true"
        android:orientation="vertical">
        <TextView
            android:id="@+id/textview_number1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="图书编号:"
            android:layout_alignParentLeft="true"
            android:textColor="#000000"
            android:textSize="23dip" />
        <EditText
            android:id="@+id/book_number1"
            android:layout_width="200dip"
            android:layout_height="wrap_content"
            android:inputType="text"
            android:layout_marginLeft="110dp"
            android:textColor="#000000"/>
    </RelativeLayout>
    <RelativeLayout
        android:id="@+id/Btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/menu"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="55dip">
        <TextView
            android:id="@+id/textview_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="图书名称:"
            android:layout_alignParentLeft="true"
            android:textColor="#000000"
            android:textSize="23dip" />
        <EditText
            android:id="@+id/book_name1"
            android:layout_width="200dip"
            android:layout_height="wrap_content"
            android:inputType="text"
            android:layout_marginLeft="110dp"
            android:textColor="#000000"/>
    </RelativeLayout>
    <RelativeLayout
        android:id="@+id/Btn3"
        android:layout_below="@id/menu"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="90dip">
        <TextView
            android:id="@+id/author_name3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="作者姓名:"
            android:layout_alignParentLeft="true"
            android:textColor="#000000"
            android:textSize="23dip" />
        <EditText
            android:id="@+id/author_name4"
            android:layout_width="200dip"
            android:layout_height="wrap_content"
            android:inputType="text"
            android:layout_marginLeft="110dp"
            android:textColor="#000000"/>
    </RelativeLayout>
        <TableLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="180dp"
            android:layout_centerHorizontal="true">
            <TableRow>
                <Button
                    android:id="@+id/increace"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="新增一行到数据库"
                    android:textSize="25px"
                    android:layout_weight="1"/>
                <Button
                    android:id="@+id/increace1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="新增到数据库"
                    android:textSize="35px"/>
            </TableRow>
        </TableLayout>
    </RelativeLayout>

    <Button
        android:layout_gravity="center"
        android:id="@+id/save"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="保存数据"
        android:textSize="35px"/>
</LinearLayout>

新增系统页面设置代码如下:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <PreferenceCategory
        android:title="系统设置">
        <EditTextPreference
            android:title="昵称"
            android:key="nicheng"/>
        <SwitchPreference
            android:title="自启动"
            android:key="auto"/>
    </PreferenceCategory>
</PreferenceScreen>

对应系统页面的java代码如下

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.PreferenceActivity;

public class Setting extends PreferenceActivity {
    public void onCreate(Bundle saveInstence) {
        super.onCreate(saveInstence);
        addPreferencesFromResource(R.xml.setting);
        SharedPreferences sp = getSharedPreferences("tes",Setting.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        String nicheng = sp.getString("nicheng","");
        EditTextPreference etp = (EditTextPreference) findPreference("nicheng");
        etp.setText(nicheng);
        editor.putString("key1",etp.toString());
        editor.commit();
    }
}

希望点击软件先进入首页界面,首页布局文件如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/menu"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="首页"
        android:textSize="35dp"
        android:textColor="#000"
        android:background="#6CF">
    </TextView>
    <TableLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center">
        <TableRow>
            <Button
                android:id="@+id/search"
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:text="查询"
                android:textColor="#fff"
                android:textSize="30dp">
            </Button>

            <Button
                android:id="@+id/newly"
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:text="新增"
                android:textColor="#fff"
                android:textSize="30dp">
            </Button>
        </TableRow>
    </TableLayout>
</LinearLayout>

相应的java文件:

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class Home_Page extends Activity {
    private Button search;
    private Button newly;
    static int A;
    private Button setting;

    public void onCreate(Bundle saveInstanceState) {
        super.onCreate(saveInstanceState);
        setContentView(R.layout.home_page);
        onClick l = new onClick(this);
        search = (Button)findViewById(R.id.search);
        newly = (Button)findViewById(R.id.newly);
        search.setOnClickListener(l);
        newly.setOnClickListener(l);


        if(A!=0) {

//        Intent i = getIntent();
//        String message = i.getStringExtra("a1")+","+i.getStringExtra("a2")+","+i.getStringExtra("a3");
//        Toast.makeText(this,message,Toast.LENGTH_SHORT).show();
            Intent intent = getIntent();
            Bundle bundle = intent.getBundleExtra("b1");
            Book book = (Book) bundle.getSerializable("a1");
            String message1 = book.bid + "," + book.bname + "," + book.author;
            Toast.makeText(this, message1, Toast.LENGTH_LONG).show();
        }
    }
    public boolean onCreateOptionsMenu(android.view.Menu menu){
        menu.add(0,1,0,"关于");
        menu.add(0,2,0,"退出");
        menu.add(0,3,0,"").setIcon(R.mipmap.ic_setting);
        return true;
    }
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case 1:
                AlertDialog.Builder a = new AlertDialog.Builder(Home_Page.this);
                a.setTitle("关于1.0版本");
                a.setMessage("2020.10.11 第一次更新 \n 内容:增添菜单栏 \n2020.10.15 第二次更新 \n 内容:增添“清空”属性 \n2020.10.22 " +
                        "第三次更新 \n 内容:增添页面转换及Intent和Bundle方法的数据传递 \n2020.10.30  第四次更新 \n 内容:选项菜单中增添系统选项,并跳转到可输入及选择的系统界面 \n " +
                        "新增页面输入的数据以Preference的方式存储起来 \n");
                a.setPositiveButton("OK",null);
                a.create();
                a.show();
                break;
            case 2:
                this.finish();
                break;
            case 3:
                Intent intent = new Intent(this,Setting.class);
                startActivity(intent);
        }
        return true;
    }
}
  1. 这里做了一些调整,把原本在查询界面的选项菜单搬了过来,但是不知道为什么目前还存在的一个bug就是当我希望case3能显示我放进去的图片,总是显示不粗来,过段时间再来深究。
  2. 并且调整了一下这里的代码,首页里点击查询可跳转到查询界面,查询界面点击返回可跳转回首页,在查询界面增添如下代码;
private Button bq;
  bq = (Button)findViewById(R.id.btn3);
  bq.setOnClickListener(l);

新增图书页面布局文件如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    <TextView
        android:id="@+id/menu"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="新增图书"
        android:textSize="35dp"
        android:textColor="#000"
        android:background="#6CF">
    </TextView>
    <RelativeLayout
        android:id="@+id/Btn1"
        android:layout_below="@id/menu"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dip"
        android:layout_centerHorizontal="true"
        android:orientation="vertical">
        <TextView
            android:id="@+id/textview_number1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="图书编号:"
            android:layout_alignParentLeft="true"
            android:textColor="#000000"
            android:textSize="23dip" />
        <EditText
            android:id="@+id/book_number1"
            android:layout_width="200dip"
            android:layout_height="wrap_content"
            android:inputType="text"
            android:layout_marginLeft="110dp"
            android:textColor="#000000"/>
    </RelativeLayout>
    <RelativeLayout
        android:id="@+id/Btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/menu"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="55dip">
        <TextView
            android:id="@+id/textview_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="图书名称:"
            android:layout_alignParentLeft="true"
            android:textColor="#000000"
            android:textSize="23dip" />
        <EditText
            android:id="@+id/book_name1"
            android:layout_width="200dip"
            android:layout_height="wrap_content"
            android:inputType="text"
            android:layout_marginLeft="110dp"
            android:textColor="#000000"/>
    </RelativeLayout>
    <RelativeLayout
        android:id="@+id/Btn3"
        android:layout_below="@id/menu"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="90dip">
        <TextView
            android:id="@+id/author_name3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="作者姓名:"
            android:layout_alignParentLeft="true"
            android:textColor="#000000"
            android:textSize="23dip" />
        <EditText
            android:id="@+id/author_name4"
            android:layout_width="200dip"
            android:layout_height="wrap_content"
            android:inputType="text"
            android:layout_marginLeft="110dp"
            android:textColor="#000000"/>
    </RelativeLayout>
        <TableLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="180dp"
            android:layout_centerHorizontal="true">
            <TableRow>
                <Button
                    android:id="@+id/increace"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="新增一行到数据库"
                    android:textSize="25px"
                    android:layout_weight="1"/>
                <Button
                    android:id="@+id/increace1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="新增到数据库"
                    android:textSize="35px"/>
            </TableRow>
        </TableLayout>
    </RelativeLayout>

    <Button
        android:layout_gravity="center"
        android:id="@+id/save"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="保存数据"
        android:textSize="35px"/>
</LinearLayout>

相应java文件如下,可实现从首页跳转过来,点击任意一个“新增xx”键可跳转回首页,并显示内容:

package com.example.bookcollection;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

/**
 * Created by Administrator on 2020/10/22.
 */
public class Newly extends Activity {
    private Button increace;
    private Button increace1;
    private Button save;
    public void onCreate(Bundle saveInstanceState) {
        super.onCreate(saveInstanceState);
        setContentView(R.layout.newly);
        increace = (Button)findViewById(R.id.increace);
        increace1 = (Button)findViewById(R.id.increace1);
        onClick lll= new onClick(this);
        increace.setOnClickListener(lll);
        increace1.setOnClickListener(lll);

        save = (Button) findViewById(R.id.save);
        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EditText e1 = (EditText) findViewById(R.id.book_number1);
                EditText e2 = (EditText) findViewById(R.id.book_name1);
                EditText e3 = (EditText) findViewById(R.id.author_name4);
                SharedPreferences sp = getSharedPreferences("test",Newly.MODE_PRIVATE);
                SharedPreferences.Editor editor = sp.edit();
                editor.putString("key1",e1.getText().toString());
                editor.putString("key2",e2.getText().toString());
                editor.putString("key3",e3.getText().toString());
                editor.commit();
            }
        });
    }
}

onClick承担监听控件被点击后作出,页面跳转(权力交接)任务,新增内容如下:

if (v.getId()==R.id.btn3) {
            Intent i = new Intent();
            i.setClass(a,Home_Page.class);
            a.startActivity(i);
        }
        if (v.getId()==R.id.search) {
            Intent i = new Intent();
            i.setClass(a,BookCollection.class);
            a.startActivity(i);
        }
        if (v.getId()==R.id.newly) {
            Intent i = new Intent();
            i.setClass(a,Newly.class);
            a.startActivity(i);
        }
        if (v.getId()==R.id.increace) {
            Intent i = new Intent(a,Home_Page.class);
            EditText e1 = (EditText) a.findViewById(R.id.book_number1);
            EditText e2 = (EditText) a.findViewById(R.id.book_name1);
            EditText e3 = (EditText) a.findViewById(R.id.author_name4);
            i.putExtra("a1",e1.getText().toString());
            i.putExtra("a2",e2.getText().toString());
            i.putExtra("a3",e3.getText().toString());
            a.startActivity(i);
        }
        if (v.getId()==R.id.increace1) {
            Home_Page.A = 1;
            Intent i = new Intent(a,Home_Page.class);
            EditText e1 = (EditText) a.findViewById(R.id.book_number1);
            EditText e2 = (EditText) a.findViewById(R.id.book_name1);
            EditText e3 = (EditText) a.findViewById(R.id.author_name4);
            Book book = new Book();
            book.bid = e1.getText().toString();
            book.bname = e2.getText().toString();
            book.author = e3.getText().toString();
            Bundle bundle = new Bundle();
            bundle.putSerializable("a1", book);
            i.putExtra("b1",bundle);
            a.startActivity(i);
        }

*补充:*之前有弄不懂为什么有时候某些控件会显示不出来需要先检查:

  1. 上一个控件的长宽是“match_parent”还是“wrap_content”。(遇到布局和自己想要的效果不一样,也可以多推理一下这些的逻辑
  2. 或者在最外层布局中增添一句话 android:orientation="vertical"
  3. 上次增添菜单的时候,出现问题,必须要try{ }catch(){ },抛出异常语句才能正常进行,就是因为如下代码
            Intent intent = getIntent();
            Bundle bundle = intent.getBundleExtra("b1");
            Book book = (Book) bundle.getSerializable("a1");
            String message1 = book.bid + "," + book.bname + "," + book.author;
            Toast.makeText(this, message1, Toast.LENGTH_LONG).show();

出现了下面这个错误java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.Serializable android.os.Bundle.getSerializable(java.lang.String)' on a null object reference(Activity之间传送对象Bundle为null的问题),但是本人笨拙,只是跟着本学期的课程学习java,所以选择了设置判断(判断静态A的值)。正好这周老师多次强调四种类型变量的情况,尝试过几次最后选择,在Home_Page.class中加入语句 static int A;在onClick.class中调用这个静态变量,加上语句Home_Page.A=1;(private只能在本包内可访问,我定义变量语句是定义为默认,默认状态下第三方类同包可访问,好啦顺便把java的知识复习了一下。)

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值