Android 使用数组传递edittext值

Android 使用数组传递edittext值,android,arrays,android-intent,android-edittext,textview,Android,Arrays,Android Intent,Android Edittext,Textview,我有三条以上的文字。当我在edittext中输入内容时,我需要将其保存到另一个屏幕。我问了一些问题,得到了答案。但是如何通过数组。我对每个edittext使用了单独的putExtra方法,另一个屏幕需要显示一个TextView。现在,我为每个视图创建了单独的文本视图 代码: 活动: et=(EditText)findViewById(R.id.et); et1=(EditText)findViewById(R.id.et1); btn=(Bu

我有三条以上的文字。当我在edittext中输入内容时,我需要将其保存到另一个屏幕。我问了一些问题,得到了答案。但是如何通过数组。我对每个edittext使用了单独的putExtra方法,另一个屏幕需要显示一个TextView。现在,我为每个视图创建了单独的文本视图

代码:

活动:

 et=(EditText)findViewById(R.id.et);

            et1=(EditText)findViewById(R.id.et1);

            btn=(Button)findViewById(R.id.btn);

            btn.setOnClickListener(new OnClickListener(){

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    Intent intent = new Intent(Save.this, Get.class);
                String[] myStrings = new String[] {"et.getText().toString()", "et1.getText().toString()"};
                intent.putExtra("strings", myStrings);
                startActivity(intent);


SharedPreferences preferences = getDefaultSharedPreferences(this);
                  SharedPreferences.Editor editor = preferences.edit();
                  editor.putString("Name","test");
                  editor.commit();

                    }


            });
活动1:

    Intent intent = getIntent();
              String[] myStrings = intent.getStringArrayExtra("strings");

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
          String name = preferences.getString("Name","");


              txt=(TextView)findViewById(R.id.txt);
                txt.setText(myStrings); 

我会写一个完整的答案

Intent intent = new Intent(Save.this, Get.class);
String[] myStrings = new String[] { et.getText().toString() ,  et1.getText().toString() };
intent.putExtra("strings", myStrings);
startActivity(intent);
然后

Intent intent = getIntent();
String[] myStrings = intent.getStringArrayExtra("strings");

txt1=(TextView)findViewById(R.id.txt1);
txt1.setText(myStrings[0]);

txt2=(TextView)findViewById(R.id.txt2);
txt2.setText(myStrings[1]); 
或者,您可以将字符串连接起来并将其传递给单个TextView

Intent intent = getIntent();
String[] myStrings = intent.getStringArrayExtra("strings");

String joined = myStrings[0] + " - " + myStrings[1];

txt=(TextView)findViewById(R.id.txt);
txt.setText(joined);
希望这会有所帮助


通常我会编写一个类,用一些静态方法处理SharedPreferences,如:

public class Storage {

    public static String getName(Context context) {
        final SharedPreferences prefs = context.getSharedPreferences("com.my.package", Context.MODE_PRIVATE);
        return prefs.getString("name", "");
    }

    public static void setName(Context context, String name) {
        final SharedPreferences prefs = context.getSharedPreferences("it.enrichman.bolloauto", Context.MODE_PRIVATE);
        prefs.edit().putString("name", name).commit();
    }

}

您可以尝试在第一次活动中存储它,然后在第二次活动中检索它。使用Toast进行测试。

此代码工作正常

    scrollview=(ScrollView)findViewById(R.id.scrollview1); 
    tb2.setTextSize(30); 
    tb2.setMovementMethod(new ScrollingMovementMethod());
    scrollview.post(new Runnable() { 
    public void run() { 
        scrollview.fullScroll(View.FOCUS_DOWN);  
        }
    }); 
    chat1.setOnClickListener(new OnClickListener() { 
        public void onClick(View v) { 
         {
             textsaring1=edt1.getText().toString();
             tb2.setText(tb2.getText()+" "+textsaring1);
             edt1.setText(" ");
        } 
        }
    }); 
}

setText的可能重复给出了错误:类型TextView中的方法setText(CharSequence)不适用于参数(String[])@Enrichman:请参见编辑的代码
myStrings
是一个数组,因此您必须以
myStrings[0]
myStrings[1]
等形式访问值。我不明白。写入位置?TextView仅显示et.getText().toString()、et1.getText().toString()这些文本不显示im写入的内容您可以使用单个TextView。编辑:)查看我的代码。您已将
et1.getText().toString()
用双引号括起来,如
“et1.getText().toString()”
。你不必!它起作用了。这是保存到某个地方。我需要稍后获取此保存数据。您可以将此数据保存在SharedReferences中。