2013-05-06 26 views
1

我刚开始编写Android应用程序。我已经完成了一些初学者的教程,但是我无法找到任何有用答案的下一步。如何在视图之间传输数据?

我想写一个计算复杂的函数,它依赖于三个参数的值的应用程序。这个作品,如果我把一个单一的视图的一切: 对于每一个参数一个文本字段(与参数名)加一个的EditText字段中输入值。底部有一个按钮,点击时,结果显示在另一个TextField中。

但是现在我想改进布局(再加上我想学习如何处理更复杂的结构): 两个参数通常只设置一次,然后保持其值。所以我决定将这两个参数移到不同的视图。

没有我要找的建议(希望链接示例代码): 哪种结构,我应该使用?似乎我不需要使用不同的活动,但是有不同意见的活动应该完成这项工作。 ViewSwitcher听起来很合理,因为在这种情况下我只需要两个不同的视图。另一方面,ViewFlipper可能更可取,因为我可以稍后在其他具有多个视图的项目中重复使用它。我还读了ViewPager允许在不同视图之间滑动。

我怎样才能读取EditField中,如果这是一个不同的看法? 我试图使用ViewSwitcher,当我添加一个额外的按钮切换到第二个视图时,它工作。但是,当我提出了一个参数和两个 第二种观点的TextField和EditField中,应用程序不会在模拟器上运行,只是说“在应用程序错误”(Eclipse的同时,显示没有错误)。从这里,我想我必须做一些额外的工作来在不同视图之间传递EditText中的数据。 我还没有找到任何例子 - 任何人都可以给我一些建议/例子吗?

任何帮助表示赞赏


我计算过,我最初的描述是也许不是没有任何代码太大的帮助。 这里是我的布局

<?xml version="1.0" encoding="utf-8"?> 
<ViewSwitcher xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/viewSwitcher1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#FFFFFF" 
    android:orientation="vertical" > 

    <LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 
    <TextView android:id="@+id/pg1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="1st view" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" > 
    <TextView android:id="@+id/v1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="value1" /> 
    <EditText android:id="@+id/val1" 
     android:text="2.0" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:inputType="numberDecimal" /> 
    </LinearLayout> 

     <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" > 
    <TextView android:id="@+id/v2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="value2" /> 
    <EditText android:id="@+id/val2" 
     android:text="3.0" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:inputType="numberDecimal" /> 
    </LinearLayout> 

    <LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 
    <Button android:layout_width="wrap_content" 
    android:id="@+id/calc" 
    android:layout_height="wrap_content" 
    android:text="2*Val1 + Val2 =" /> 
    <TextView android:id="@+id/res" 
     android:text="" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textStyle="bold" 
     android:textSize="24sp" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" > 
    <Button android:id="@+id/b1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="switch view 1" />  
    </LinearLayout> 
    </LinearLayout> 

<!-- next view  --> 

    <LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#FFFFFF" 
    android:orientation="vertical" > 

    <LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 
    <TextView android:id="@+id/pg2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="View 2" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" > 
    <Button android:id="@+id/b2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="switch view 2" />  
    </LinearLayout> 
    </LinearLayout> 

</ViewSwitcher> 

,这里是我的主要活动:

package com.example.testas2; 

import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 
import android.widget.ViewSwitcher; 

public class MainActivity extends Activity { 

    ViewSwitcher switcher; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     switcher = (ViewSwitcher)findViewById(R.id.viewSwitcher1); 
     initControls(); 
    } 

     private void initControls() 
     { 
      Button calculate=(Button)findViewById(R.id.calc); 
      calculate.setOnClickListener(new Button.OnClickListener() 
      {public void onClick (View v) { calculate(); }}); 

      Button b1=(Button)findViewById(R.id.b1); 
      b1.setOnClickListener(new Button.OnClickListener() 
      {public void onClick (View v) { switcher.showNext(); }}); 

      Button b2=(Button)findViewById(R.id.b2); 
      b2.setOnClickListener(new Button.OnClickListener() 
      {public void onClick (View v) { switcher.showPrevious(); }}); 

      calculate(); 
     } 

     private void calculate() 
     { 
      EditText val1=(EditText)findViewById(R.id.val1); 
      EditText val2=(EditText)findViewById(R.id.val2); 
      TextView res=(TextView)findViewById(R.id.res); 
      Double Val1=Double.parseDouble(val1.getText().toString()); 
      Double Val2=Double.parseDouble(val2.getText().toString()); 
      Double result = 2*Val1+Val2; 
      res.setText(String.format("%.3f", result)); 
     } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

} 

这个版本工作: - 按下第一个按钮时,它计算:结果= 2 *值1 +值2) - 推动按钮“开关视图1”时,它前进到所述第二视图 时 - 在所述第二视图中,按下按钮“开关视图2”时,它返回到图1

但我不能得到foll由于两件工作: 1)我想移动的“开关视图”按钮,第一个视图的顶部,但是这给在模拟器中的错误(“不幸的是testas2已停止”)。 2)我想将第二个值的输入移动到第二个视图,但是这会给模拟器带来相同的错误。

我在做什么错? 为什么布局中元素的顺序很重要,以及如何修改我的代码以使其工作?

+0

发表什么logcat显示当模拟器统计“应用程序中的错误” – 2013-05-07 05:43:11

+0

像卡兰说。如果你给我们确切的错误信息,这将有所帮助。看看你的logcat窗口,了解你的错误的完整解释。 – Bram 2013-05-07 16:07:34

+0

在LogCat窗口中,它声明:“java.lang.ClassCastException:android.widget.TextView无法转换为android.widget.Button” – Markus 2013-05-07 16:16:25

回答

1

我只是做了“项目>清洁”,重新启动了模拟器中的应用程序,现在它的工作原理!
换句话说,原始问题的解决方案是微不足道的(实际上并没有真正的问题)。可以简单地在视图内或视图之间移动布局文件(TextView,EditText,Button)中的元素。发布的活动始终有效,并且代码中的任何地方都可以访问数据(来自不同的TextView和EditView字段)。因此,不需要在视图之间明确的“传输”数据。
只有在布局文件中移动元素后,才能执行“项目>清理”。

1

示例代码 -

TextView tField= (add cast here)findViewById(R.id.editText) 

tfield.getText(); 

,如果你发布你的一些代码,因为findViewById有时取决于容器视图和上面的代码将无法再工作,因为它会寻找以上回答可以改善textfield在默认视图中。在新的活动

Intent i = new Intent(getApplicationContext(), NewActivity.class); 
i.putExtra("new_variable_name","value"); 
startActivity(i); 

然后检索这些值:

+0

谢谢!我现在提供了更具体的信息,提供布局和主要活动。 – Markus 2013-05-07 16:07:02

0

在你当前活动,创建一个新的意向

Bundle extras = getIntent().getExtras(); 
if (extras != null) { 
    String value = extras.getString("new_variable_name"); 
} 

欲了解更多信息请按照本link

+0

谢谢!但我不认为这项任务需要引入第二项活动 - 一种不同意见的活动应该有效。 – Markus 2013-05-07 01:51:41