Android Studio学习记录之简单的页面切换及宫格菜单

之前上课听老师讲的一些东西自己其实并没有消化,今天把不懂的都去网上搜了一下,有了一种恍然大悟的感觉,包括很多方方面面的东西。有些东西听说了也明白,但是没有实际的操作就还是不能够知道具体场合应该怎么用。虽然不能面面俱到把所有的东西都搞透,但是通过整理的过程,希望把我理解的东西表达出来,也希望以后回过来看的时候,能够有进一步的理解。

1.首先是MainActivity.java,一个主界面

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private static final String TAG = "MainActivity";//定义静态常量

    private Button button,button1;

    private EditText editText;
    //Activity初始化顺序,onCreate(),onStart(),onReaume()
    @Override
    protected void onCreate(Bundle savedInstanceState) {//保护当时的状态,以防被kill掉
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate() called with: savedInstanceState = [" + savedInstanceState + "]");
        setContentView(R.layout.activity_main);
        //登录
        button = (Button)findViewById(R.id.button);
        //注册
        button1 = (Button)findViewById(R.id.button2);

        editText = (EditText)findViewById(R.id.editText5);
        button.setOnClickListener(this);
        button1.setOnClickListener(this);

    }


    @Override
    protected void onStart() {
        super.onStart();
        Log.d(TAG, "onStart() called");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG, "onResume() called");
    }
     //Activity销毁顺序:onPause(),onStop(),onDestroy()
    @Override
    protected void onPause() {
        super.onPause();
        Log.d(TAG, "onPause() called");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d(TAG, "onRestart() called");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy() called");
    }


    @Override
    //可以将多个View的onClick事件写到一个onClick事件中,当事件发生时,参数v就指向当前点击的View
    public void onClick(View v) {
        String show = editText.getEditableText().toString();
        //Intent:意图(一个Activity启动另一个Activity,需要startActivity()函数,函数的参数是Intent对象)
        //取Intent对象

        //初始化
        Intent intent = getIntent();

        switch (v.getId()){
            case R.id.button: //登录  R文件下的
                //设置需要跳转的activity
                intent.setClass(this,LoginActivity.class);
                //bundle对象是用来数据传递
                //创建bundle对象
                Bundle bundle = new Bundle();
                //把需要传的数据放入bundle
                bundle.putString("show",show);
                //把bundle放入intent(进行封装)
                intent.putExtras(bundle);
                //跳转
                startActivity(intent);
                break;
            case R.id.button2://注册
                intent.setClass(this,RegisterActivity.class);
                startActivity(intent);
                break;
        }
    }
}

这次就是比昨天更进一步理解了Activity的生命周期,以及为何继承AppCompatActivity,这个会比Activity多一个标签,有一些区别。
样式也放一下

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.example.k42.myapplication.MainActivity">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/lux"
        android:id="@+id/imageView"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true" />

    <Button
        android:text="注册"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/button"
        android:layout_alignParentEnd="true"
        android:layout_marginEnd="64dp"
        android:id="@+id/button2" />

    <Button
        android:text="登录"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="27dp"
        android:id="@+id/button"
        android:layout_alignParentBottom="true"
        android:layout_toStartOf="@+id/button2"
        android:layout_marginEnd="24dp" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:hint="请输入学校"
        android:ems="10"
        android:layout_above="@+id/button2"
        android:layout_alignStart="@+id/button"
        android:layout_marginBottom="16dp"
        android:id="@+id/editText5" />
</RelativeLayout>

效果图

android studio滑动切换界面 android studio页面切换_Android开发

2. 登录注册

这个也没啥好提的,只是我要借助这个页面跳转到菜单,下面是代码
RegisterActivity.java

public class RegisterActivity extends Activity implements View.OnClickListener{

    private static final String TAG = "RegisterActivity";

    private EditText editText,editText1,editText2;

    private Button button,button1;

    private String name,password,phone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);
        Log.d(TAG, "onCreate() called ");

        editText = (EditText)findViewById(R.id.editText);
        editText1 = (EditText)findViewById(R.id.editText1);
        editText2 = (EditText)findViewById(R.id.editText2);

        button = (Button)findViewById(R.id.button5);
        button1 = (Button)findViewById(R.id.button6);

        //给button按钮设置单击事件的监听器
        button.setOnClickListener(this);
        button1.setOnClickListener(this);
    }



    /**
     * 监听单击事件的方法
     * @param v
     */
    @Override
    public void onClick(View v) {
        Log.d(TAG, "onClick() called with: v = [" + v + "]");
        switch (v.getId()){
            case R.id.button5: //点击的是确认按钮
                name = editText.getText().toString();
                password = editText1.getText().toString();
                phone = editText2.getText().toString();
                Log.d(TAG, name+","+password+","+phone);
                //显示提示信息
                //第一个参数: 上下文,当前类
                //第二个参数: 显示的提示信息
                //第三个参数: 显示的时间长短
                Toast.makeText(this,name+","+password+","+phone,Toast.LENGTH_SHORT).show();
                break;
            case R.id.button6: //点击的是返回按钮
                break;
        }
    }
}

样式register.xml

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

    <TextView
        android:text="注册"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView2"
        android:layout_gravity="center"
        android:textSize="30sp"
        android:layout_marginTop="10dp"
        />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="姓名:"
            android:layout_marginLeft="10dp"/>

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/editText"
            android:hint="请输入姓名"
            />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="密码:"
            android:layout_marginLeft="10dp"/>

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/editText1"
            android:hint="请输入密码"
            android:inputType="textPassword"
            />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="手机:"
            android:layout_marginLeft="10dp"/>

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/editText2"
            android:hint="请输入手机"
            android:inputType="phone"
            />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center"
        >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="确定"
            android:id="@+id/button5"
            android:background="@color/colorPrimary"
             />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="返回"
            android:id="@+id/button6"
            />

    </LinearLayout>

</LinearLayout>

效果图

android studio滑动切换界面 android studio页面切换_Text_02


登录Login.java

public class LoginActivity extends Activity implements View.OnClickListener{

    private Button button,button1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);

        button = (Button)findViewById(R.id.button7);
        button1 = (Button)findViewById(R.id.button8);

        button.setOnClickListener(this);
        button1.setOnClickListener(this);

        //取intent对象
        Intent intent = getIntent();
        //取bundle对象
        Bundle bundle = intent.getExtras();
        //从bundle对象中取数据
        String show = bundle.getString("show");
        Toast.makeText(this,show,Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onClick(View v) {
        Intent intent = getIntent();

        switch (v.getId()){
            case R.id.button7:
                intent.setClass(this,MenuActivity.class);
                startActivity(intent);
                break;
            case R.id.button8:
                intent.setClass(this,MainActivity.class);
                startActivity(intent);
                break;
        }
    }
}

样式

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

    <TextView
        android:text="登录"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:textSize="30sp"
        android:layout_marginTop="10dp"
        />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="用户名:"
            android:layout_marginLeft="10dp"/>

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/editText3"
            android:hint="请输入用户名"
            />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="密码:"
            android:layout_marginLeft="10dp"/>

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/editText4"
            android:hint="请输入密码"
            android:inputType="textPassword"
            />
    </LinearLayout>



    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center"
        >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="确定"
            android:id="@+id/button7"
            />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="返回"
            android:id="@+id/button8"
            />

    </LinearLayout>

</LinearLayout>

效果图

android studio滑动切换界面 android studio页面切换_ide_03

3.这次的重点是菜单

这一块由三个部分组成,用的是MVC模式,即model,view,control.下面的代码中 menu.xml负责菜单界面的布局,grid_item.xml负责单个图标的显示样式,MenuActivity.java中的Adapter适配器负责控制。

menu.xml

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

    <!--numColumns: 每一行多少个
        horizontalSpacing:第一行的每个单元格之间的间距
        verticalSpacing: 行间距
    -->
    <GridView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:numColumns="3"
        android:layout_marginTop="10dp"
        android:horizontalSpacing="20dp"
        android:verticalSpacing="20dp"
        android:gravity="center"
        android:id="@+id/grid_view"
        />
</LinearLayout>

注:这里的gridview是表示宫格显示。

效果图

android studio滑动切换界面 android studio页面切换_android_04


grid_item.xml

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

    <ImageView
        android:layout_width="60dp"
        android:layout_height="60dp"
        app:srcCompat="@drawable/a"
        android:layout_centerHorizontal="true"
        android:id="@+id/imageView2" />

    <TextView
        android:text="TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageView2"
        android:layout_marginTop="5dp"
        android:layout_centerHorizontal="true"
        android:id="@+id/textView3" />
</RelativeLayout>

效果图

android studio滑动切换界面 android studio页面切换_ide_05


MenuActivity.java

public class MenuActivity extends Activity implements AdapterView.OnItemClickListener{

    private static final String TAG = "MenuActivity";

    //gridView宫格形式
    private GridView gridView;

    //定义菜单项的图标
    private int[] icons = {R.drawable.menu1,R.drawable.menu2,R.drawable.menu3,
                            R.drawable.menu4,R.drawable.menu5,R.drawable.menu6};

    //定义菜单项的标题
    private String[] titles = {"宠物","花花","新闻","天气","游戏","音乐"};

    //定义一个适配器
    private SimpleAdapter simpleAdapter;

    //gridview需要显示的所有的菜单数据
    private List<Map<String,Object>> data;

    @Override//重写父类的方法
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.menu);

        //findViewById最终调用ViewGroup中的findViewTraversal,这个方法会遍历所有的子View
        gridView = (GridView)findViewById(R.id.grid_view);

        init();
        //第一个参数: 上下文
        //第二个参数: 需要显示的数据
        //第三个参数:每一个选项显示的布局文件
        //第四个参数: list中hashmap的键构造的数组
        //第五个参数: 第四个参数数组中的键的值对应的需要显示的组件ID
        simpleAdapter = new SimpleAdapter(this,data,R.layout.grid_item,
                new String[]{"icon","title"},new int[]{R.id.imageView2,R.id.textView3});

        //绑定适配器
        gridView.setAdapter(simpleAdapter);
        //设置监听器
        gridView.setOnItemClickListener(this);
    }



    /**
     * 初始化菜单项的数据
     */
    public void init(){
        data = new ArrayList<>();//动态数组
        //遍历图标数组
        for(int i=0;i<icons.length;i++){
            //构造每一个菜单项的HashMap
            Map<String,Object> item = new HashMap<>();
            item.put("icon",icons[i]);
            item.put("title",titles[i]);
            data.add(item);
        }
        Log.d(TAG, data.toString());
    }

    /**
     * 每一个选项点击事件触发的方法
     * @param parent
     * @param view
     * @param position
     * @param id
     */
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = getIntent();
        switch (position) {
            case 0:
                intent.setClass(this,PetActivity.class);
                startActivity(intent);
                break;
            case 1:
                intent.setClass(this,FlowerActivity.class);
                startActivity(intent);
                break;
            case 2:
                break;
            case 3:
                break;
            case 4:
                break;
            case 5:
                break;

        }

    }
}

终极效果

android studio滑动切换界面 android studio页面切换_ide_06


使用的是海马玩的模拟器,其中可以发现在MenuActivity中的HashMap是用来遍历每一组图标和标题的。

4.点击图标后做出回应

我们以第一个图标为例,点击宠物,使它做出回应。

public class PetActivity extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

       //提示信息
        Toast.makeText(this,"这是宠物",Toast.LENGTH_SHORT).show();
    }
}

至此,关于菜单部分的整理结束了。

其中有一点要特别说一下,每做一个页面,要让它能够显示出来,一定要去main下的AndroidManifest.xml中注册一下(如下代码)

<activity android:name=".PetActivity" />

否则,在模拟器上会显示

android studio滑动切换界面 android studio页面切换_android_07


完整部分如下

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.k42.myapplication" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >
        <activity android:name=".MainActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <!-- 注册activity -->
        <activity android:name=".LoginActivity" />
        <activity android:name=".RegisterActivity" />
        <activity android:name=".MenuActivity" />
        <activity android:name=".PetActivity" />
        
    </application>


</manifest>