2017-06-17 59 views
0

这是我在这里的第一篇文章,我对编程非常陌生,我在android studio中开始了一个项目,但是我陷入了一些困难,因为我缺乏的知识。使用gridlayout从列中获取变量和数字的总和

我有三个不同的选项卡,我想对第二个选项进行计算。

所以我的问题是我如何计算gridlay内的列中的数据?

我的组件树是这个样子的TAB2:

的RelativeLayout>滚动视图>网格布局。

我在第2列和第3列插入了几个textedits,我的目标是根据它们的输入进行计算,然后将它们作为结果插入到选项卡底部。

第2列仅包含数字,第3列包含变量。

我想使用户在第三列等于数字输入变量,例如A = 20 B = 17.5等

所以当其计算其自动改变可变到 相等数目。

我该怎么做?

TAB2 java类:

package com.example.minmerit; 

/** 
* Created by lukin on 2017-06-14. 
*/ 

import android.app.Activity; 
import android.support.v4.app.Fragment; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 


public class Tab2Betyg extends Fragment { 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.tab2betyg, container, false); 
     return rootView; 

    } 
} 

TAB2布局的xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="450dp" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="com.example.minmerit.MainActivity$PlaceholderFragment"> 


    <TextView 
     android:id="@+id/textview" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

    <ScrollView 
     android:id="@+id/scrollView" 
     android:layout_width="match_parent" 
     android:layout_height="600dp" 
     android:layout_below="@id/textview"> 


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

      <EditText 
       android:id="@+id/POÄNG" 
       android:layout_width="67dp" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:layout_row="1" 
       android:ems="10" 
       android:fontFamily="monospace" 
       android:inputType="number" 
       android:text="100" 
       android:textAlignment="center" 
       android:textColor="@android:color/black" /> 

回答

0

首先,给你的EditTexts一些适当的ID,像

<EditText    
android:id="@+id/number_1" 
... 

然后在如。 onViewCreated使changeListeners:

EditText number_1 = (EditText) findViewById(R.id.number_1); 
number_1.addTextChangedListener(changeListener); 
// change listeners for other fields 

TextWatcher changeListener = new TextWatcher() { 

    @Override 
    public void afterTextChanged(Editable s) {} 

    @Override  
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {} 

    @Override  
    public void onTextChanged(CharSequence s, int start, 
    int before, int count) { 
     int a = Integer.parseInt(s); 
     // do something with "a" 
    } 
    }); 
+0

首先它不能化解方法findViewById和changeListeners 我如何获得科拉姆2和3的网格布局的总和? –

+0

GridLayout仅用于内部视图的视觉排列。要总结“列”(或者确切地说:列中的EditTexts的总和值),你必须给它们ID。更何况,GridLayout缺少一些属性,比如columnCount。看看这里,也许这将有助于:http://www.techotopia.com/index.php/Working_with_the_Android_GridLayout_in_XML_Layout_Resources 请看看这个TableLayout,也许这对你会更好:https://developer.android.com/guide /topics/ui/layout/grid.html – thorin86

+0

好吧,这解释了很多哈哈,ive终于设法配置gridlayout,所以我现在不太乐意尝试改变它。 为什么我需要columncount?我已经插入了layoutrow/layoutcolumn,如果这就是你的意思。 我一直在寻找一些时间和(告诉我,如果我错了)能够做我想做的最简单的方法是创建一个SQLiteDatabase。 我已经根据它们所在的列将编辑文本设置了正确的ID。 谢谢! –