Android 计算器布局测试2

采用GridView动态布局,多个按钮样式相同,只是文字不同,采用这种方式可以省事一些,技术含量更高。

private void initTextBtns() {
         for (String btn : mTextBtns) {
             mTextBtnsList.add(btn);
         }
     }

参考资料:http://blog.csdn.net/like_program/article/details/51813632


1. 修改xml文件(只写xml,在界面显示不出按钮,还需要代码来生成。)

<?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:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="@string/app_name"
        android:textSize="18sp"
        android:gravity="center"
        android:background="#729FFA" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <!--操作界面-->
        <GridView
            android:id="@+id/grid_buttons"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_margin="10dip"
            android:gravity="center"
            android:horizontalSpacing="10dp"
            android:numColumns="4"
            android:verticalSpacing="10dp"></GridView>

        <!-- 显示界面 -->
        <EditText
            android:id="@+id/edit_input"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@id/grid_buttons"
            android:layout_alignParentTop="true"
            android:gravity="start"
            android:hint="输入表达式"
            android:padding="10dp"

            android:textSize="22sp" />

    </RelativeLayout>

</LinearLayout>

 

 

 

2. 修改java文件

 

package com.example.buttontextviewtest;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.GridView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    /**     * 操作按钮     */
    private GridView mGridView = null;
    /**     * 输入框     */
    private EditText mEditInput = null;
    /**     * 适配器     */
    private ArrayAdapter mAdapter = null;
    /**     * 操作按钮上的字符集合     */
    private final String[] mTextBtns = new String[]{
            "Back","(",")","CE",
            "7","8","9","/",
            "4","5","6","*",
            "1","2","3","+",
            "0",".","=","-",
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 隐藏标题栏
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        mEditInput = (EditText) findViewById(R.id.edit_input);
        mGridView = (GridView) findViewById(R.id.grid_buttons);
        // 创建适配器
        mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mTextBtns);
        // 设置适配器
        mGridView.setAdapter(mAdapter);
    }
}

这样,动态的界面就生成了。

posted on 2017-06-13 19:28  HBU_DAVID  阅读(268)  评论(0编辑  收藏  举报

导航