(一)基本控件

AndroidStudio环境搭建参考https://www.cnblogs.com/yanglh6-jyx/p/Android_AS_Configuration.html

(一)环境基本开发操作

开发环境基本操作如下:

 

点击上图中的Text则进入文本编辑模式,如下图所示,其中id参数是唯一标记的,后续可通过此id获取到相关控件,如下图所示

在java-com.example.app0下activity_main下进行逻辑代码编写如下所示:

(二)Button控件

button是开发过程中最基本的按键

        <Button
            android:id="@+id/btn_next"
            android:layout_width="150dp"
            android:text="下一张"
            android:layout_height="wrap_content" />

(三)TextView

文字显示控件

    <TextView
        android:id="@+id/tv_show"
        android:gravity="center"
        android:text="图片信息"
        android:textColor="@color/colorPrimary"
        android:layout_width="match_parent"
        android:layout_height="50dp" />

 

 (四)ImageView图片显示控件

    <ImageView
        android:id="@+id/iv_show"
        android:layout_width="match_parent"
        android:layout_height="393dp"
        android:layout_weight="1"
        android:background="@color/colorPrimaryDark" />

 

 

//-------------------------------------------------------------------------基本代码-----------------------------------------------------------------------//

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <!--<TextView-->
        <!--android:layout_width="wrap_content"-->
        <!--android:layout_height="wrap_content"-->
        <!--android:text="Hello World!" />-->

    <!--<ImageView-->
        <!--android:src="@drawable/child"-->
        <!--android:layout_width="300dp"-->
        <!--android:layout_height="300dp" />-->

    <!--<Button-->
        <!--android:layout_width="200dp"-->
        <!--android:layout_height="50dp"-->
        <!--android:textColor="@color/colorPrimary"-->
        <!--android:text="按钮" />-->
    <ImageView
        android:id="@+id/iv_show"
        android:layout_width="match_parent"
        android:layout_height="393dp"
        android:layout_weight="1"
        android:background="@color/colorPrimaryDark" />
    
    <TextView
        android:id="@+id/tv_show"
        android:gravity="center"
        android:text="图片信息"
        android:textColor="@color/colorPrimary"
        android:layout_width="match_parent"
        android:layout_height="50dp" />

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

        <Button
            android:id="@+id/btn_previous"
            android:layout_width="150dp"
            android:text="上一张"
            android:layout_height="wrap_content" />

        <Button
            android:id="@+id/btn_next"
            android:layout_width="150dp"
            android:text="下一张"
            android:layout_height="wrap_content" />

    </LinearLayout>


</LinearLayout>
package com.example.app0;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    ImageView imageView;
    TextView imageTitle;
    String[] titles;
    int[] images;
    int currentImage;

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

        initData();
    }

    private void initData()
    {
        titles=new String[]{"第1张图片","第2张图片","第3张图片","第4张图片","第5张图片"};
        images=new int[]{R.drawable.child,R.drawable.child01,R.drawable.child02,R.drawable.child03,R.drawable.child04};

        imageView.setImageResource(images[0]);
        imageTitle.setText(titles[0]);
        currentImage=0;
    }

    private void initView()
    {
        imageView = findViewById(R.id.iv_show);
        imageTitle=findViewById(R.id.tv_show);
        Button prevBtn = findViewById(R.id.btn_previous);
        Button nextBtn=findViewById(R.id.btn_next);

        prevBtn.setOnClickListener(this);
        nextBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v)
    {
        if(v.getId()==R.id.btn_next) {
            currentImage++;

            if(currentImage>4)
                currentImage=0;
        }
        else {
            currentImage--;

            if(currentImage<0)
                currentImage=4;
        }

        imageView.setImageResource(images[currentImage]);
        imageTitle.setText(titles[currentImage]);
    }
}

 

posted @ 2018-11-06 09:42  81192  阅读(222)  评论(0编辑  收藏  举报