如何从多个文本框中检索信息? (机器人)

时间:2012-10-12 12:26:16

标签: android eclipse

修改

我现在有了这段代码:

public void sendMessage(View view) {
       Intent intent = new Intent(this, DisplayMessageActivity.class);
        EditText editText = (EditText) findViewById(R.id.enter_name);
        String message = editText.getText().toString();
        intent.putExtra(EXTRA_MESSAGE, message);

        EditText editText1 = (EditText) findViewById(R.id.enter_age);
        String message1 = editText1.getText().toString();
        intent.putExtra(EXTRA_MESSAGE, message1);

        EditText editText2 = (EditText) findViewById(R.id.enter_height);
        String message2 = editText2.getText().toString();
        intent.putExtra(EXTRA_MESSAGE, message2);

        EditText editText3 = (EditText) findViewById(R.id.enter_weight);
        String message3 = editText3.getText().toString();
        intent.putExtra(EXTRA_MESSAGE, message3);

        EditText editText4 = (EditText) findViewById(R.id.enter_gender);
        String message4 = editText4.getText().toString();
        intent.putExtra(EXTRA_MESSAGE, message4);

        startActivity(intent);
    }
}

我如何同时从所有这些窗口中删除所有信息?因为我想将其保存在用户配置文件中。目前这是我的代码,但我只是从“enter_gender”获得信息。

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Get the message from the intent
        Intent intent = getIntent();
        String message = intent.getStringExtra(NutritionHandler.EXTRA_MESSAGE);

        // Create the text view
        TextView textView = new TextView(this);
        textView.setTextSize(40);
        textView.setText(message);


        // Set the text view as the activity layout
        setContentView(textView);
    }

2 个答案:

答案 0 :(得分:0)

我认为您可以通过重复代码的第3,4和5行来轻松实现它,只需更改ID。

答案 1 :(得分:0)

尝试以下,

你的Xml

    

  

<EditText
    android:id="@+id/edtName"       
    android:layout_height="40dp"    
    android:layout_below="@+id/txtName"  
    android:background="@drawable/edittext" 
    android:layout_width="fill_parent"
    android:layout_marginRight="5dip" 
    android:hint="@string/hnt_name"  
    android:layout_marginLeft="5dip"   
    android:inputType="textCapSentences"
    /> 

 <EditText
    android:id="@+id/edtAge" 
    android:layout_height="40dp"   
    android:layout_below="@+id/txtEmailid"  
    android:background="@drawable/edittext" 
    android:layout_width="fill_parent"
    android:layout_marginRight="5dip"   
    android:layout_marginLeft="5dip" 
    android:hint="@string/hnt_emailid"      
    android:inputType="textEmailAddress"    

    />

在你的Oncreate中

 EditText name = (EditText) findViewById(R.id.edtName);        
 EditText  age = (EditText) findViewById(R.id.edtAge);

获取此值

   String var_name = name.getText().toString();  
   String var_age = age.getText().toString();

希望有所帮助

相关问题