2017-05-22 34 views
-3

我一直拉着头发过去4个小时,试图找出如何做到这一点。如何从**编辑文本**中获取价值并在另一个活动的文本视图中显示

我检查了SO其他问题,但没有发现任何好处。 我是一名中级程序员,如果您的意见和答案是以我能理解的方式编写的,我会非常感激。

我希望能够把用户进入的InputPage活动@+id/location EDITTEXT框中的文本和被压在InputPage活动按钮时在@+id/ListName的TextView上InputPage2活动显示。

InputPage Java代码

public class InputPage extends Activity { 
    public String name; 
    public InputPage() { 
} 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_input_page); 
     Button button = (Button)findViewById(R.id.addButton); 
    } 


    public InputPage(String name) { 
     this.name = name; 
    } 

    public String getName() { 
     return name; 
    } 
    public String name(String name){ 
     this.name = name; 
     EditText editText = (EditText)findViewById(R.id.location); 
     String value = editText.getText().toString(); 
     return value; 

    } 

    public void toInputScreen2(View view) { 
     Button button = (Button) findViewById(R.id.button2); 
     button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Intent intent2 = new Intent(InputPage.this, InputPage2.class); 
       startActivity(intent2); 
      } 
     }); 
    } 
} 

XML

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".InputPage" 
    android:background="@color/BackgroundGrey"> 

    <TextView 
     android:id="@+id/mapNo" 
     android:layout_width="wrap_content" 
     android:layout_height="35dp" 
     android:text="@string/mapNoTextBox" 
     android:textColor="@color/textColour" 
     android:textSize="30sp" 
     android:textStyle="bold" 
     app:layout_constraintBottom_toBottomOf="parent" 
     android:layout_marginBottom="16dp" 
     android:layout_marginRight="8dp" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toTopOf="parent" 
     android:layout_marginTop="8dp" 
     android:layout_marginLeft="8dp" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintHorizontal_bias="0.04" 
     app:layout_constraintVertical_bias="0.13" /> 

    <TextView 
     android:id="@+id/inputPageTitle" 
     android:layout_width="178dp" 
     android:layout_height="21dp" 
     android:text="@string/inputPageTitle" 
     android:textSize="20sp" 
     android:textStyle="bold" 
     android:textColor="@color/textColour" 
     android:layout_marginRight="8dp" 
     app:layout_constraintRight_toRightOf="parent" 
     android:layout_marginLeft="8dp" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintTop_toTopOf="parent" 
     android:layout_marginTop="8dp" 
     app:layout_constraintBottom_toBottomOf="parent" 
     android:layout_marginBottom="8dp" 
     app:layout_constraintVertical_bias="0.01999998" /> 

    <EditText 
     android:id="@+id/noInput" 
     android:layout_width="48dp" 
     android:layout_height="39dp" 
     android:ems="10" 
     android:inputType="number" 
     android:layout_marginRight="8dp" 
     app:layout_constraintRight_toRightOf="parent" 
     android:layout_marginLeft="8dp" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintTop_toTopOf="parent" 
     android:layout_marginTop="8dp" 
     app:layout_constraintBottom_toBottomOf="parent" 
     android:layout_marginBottom="8dp" 
     app:layout_constraintVertical_bias="0.13" 
     app:layout_constraintHorizontal_bias="0.51" /> 

    <TextView 
     android:layout_width="150dp" 
     android:layout_height="38dp" 
     android:text="@string/location" 
     android:textColor="@color/textColour" 
     android:textSize="30sp" 
     android:textStyle="bold" 
     android:layout_marginRight="8dp" 
     app:layout_constraintRight_toRightOf="parent" 
     android:layout_marginLeft="8dp" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintTop_toTopOf="parent" 
     android:layout_marginTop="8dp" 
     app:layout_constraintBottom_toBottomOf="parent" 
     android:layout_marginBottom="8dp" 
     app:layout_constraintHorizontal_bias="0.041" 
     app:layout_constraintVertical_bias="0.24" /> 

    <EditText 
     android:id="@+id/location" 
     android:layout_width="177dp" 
     android:layout_height="44dp" 
     android:ems="10" 
     android:inputType="textPersonName" 
     android:hint="@string/hintText" 
     android:textColorHint="@color/hintTextColour" 
     app:layout_constraintTop_toTopOf="parent" 
     android:layout_marginTop="8dp" 
     app:layout_constraintBottom_toBottomOf="parent" 
     android:layout_marginBottom="8dp" 
     android:layout_marginRight="8dp" 
     app:layout_constraintRight_toRightOf="parent" 
     android:layout_marginLeft="8dp" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintHorizontal_bias="0.952" 
     app:layout_constraintVertical_bias="0.243" /> 

    <EditText 
     android:id="@+id/dateBox" 
     android:layout_width="133dp" 
     android:layout_height="44dp" 
     android:ems="10" 
     android:inputType="date" 
     android:hint="@string/dateBoxHint" 
     android:textColorHint="@color/hintTextColour" 
     android:layout_marginLeft="61dp" 
     app:layout_constraintLeft_toLeftOf="parent" 
     android:layout_marginRight="8dp" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintBottom_toBottomOf="parent" 
     android:layout_marginBottom="8dp" 
     app:layout_constraintTop_toTopOf="parent" 
     android:layout_marginTop="8dp" 
     app:layout_constraintHorizontal_bias="0.335" 
     app:layout_constraintVertical_bias="0.374" /> 

    <TextView 
     android:layout_width="101dp" 
     android:layout_height="34dp" 
     android:text="@string/dateBox" 
     android:textColor="@color/textColour" 
     android:textStyle="bold" 
     android:textSize="30sp" 
     app:layout_constraintTop_toTopOf="parent" 
     android:layout_marginTop="8dp" 
     app:layout_constraintBottom_toBottomOf="parent" 
     android:layout_marginBottom="8dp" 
     android:layout_marginRight="8dp" 
     app:layout_constraintRight_toRightOf="parent" 
     android:layout_marginLeft="8dp" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintHorizontal_bias="0.032" 
     app:layout_constraintVertical_bias="0.369" /> 

    <Button 
     android:id="@+id/button2" 
     android:layout_width="165dp" 
     android:layout_height="58dp" 
     android:text="@string/continueButton" 
     android:background="@drawable/buttons" 
     android:layout_marginRight="8dp" 
     android:onClick="toInputScreen2" 
     app:layout_constraintRight_toRightOf="parent" 
     android:layout_marginLeft="8dp" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintBottom_toBottomOf="parent" 
     android:layout_marginBottom="8dp" 
     app:layout_constraintTop_toTopOf="parent" 
     android:layout_marginTop="8dp" 
     app:layout_constraintHorizontal_bias="0.502" 
     app:layout_constraintVertical_bias="0.778" /> 


</android.support.constraint.ConstraintLayout> 

** InputPage2 Java代码**

public class InputPage2 extends Activity { 

    Button saveButton1; 
    EditText message; 
    String Message; 
    TextView[] pairs; 
    int num_match; 




    public TextView[] getPairs() { 
     return pairs; 
    } 

    public void setPairs(TextView[] pairs) { 
     this.pairs = pairs; 
    } 

    public int getNum_match() { 
     return num_match; 
    } 

    public void setNum_match(int num_match) { 
     this.num_match = num_match; 
    } 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_input_page2); 
     message = (EditText) findViewById(R.id.textNotAtHomes); 



    } 

    public void toSaveField(View v) { 
     saveButton1 = (Button) findViewById(R.id.saveButton1); 
     saveButton1.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
      } 

     }); 


     Message = message.getText().toString(); 
     try { 
      FileOutputStream fos = openFileOutput("Text.txt", MODE_WORLD_READABLE); 
      OutputStreamWriter osw = new OutputStreamWriter(fos); 
      try { 
       osw.write(Message); 
       osw.flush(); 
       osw.close(); 
       Toast.makeText(getBaseContext(), "Saved successfully", Toast.LENGTH_LONG).show(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } 

     Intent intent2 = new Intent(InputPage2.this, MainActivity.class); 
     startActivity(intent2); 
    } 

    public void linearLayout(TextView[] pairs, int num_match) { 
     LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linearLayout); 
     LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 
       ViewGroup.LayoutParams.WRAP_CONTENT); 
     pairs = new TextView[num_match + 1]; 
     for (int l = 1; l <= num_match; l++) { 
      pairs[1].setTextSize(15); 
      pairs[1].setLayoutParams(layoutParams); 
      pairs[1].setId(l); 
      linearLayout.addView(pairs[1]); 

     } 
    } 
} 

相关联的XML

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".InputPage2" 
    android:background="#ffffff" 
    tools:layout_editor_absoluteY="25dp" 
    tools:layout_editor_absoluteX="0dp" 
    android:backgroundTint="@color/BackgroundGrey"> 


    <Button 
     android:onClick="toSaveField" 
     android:id="@+id/saveButton1" 
     android:layout_width="341dp" 
     android:layout_height="70dp" 
     android:background="@drawable/buttons" 
     android:text="@string/saveButton" 
     android:textSize="20dp" 
     android:textStyle="bold" 
     android:layout_marginLeft="8dp" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintBottom_toBottomOf="parent" 
     android:layout_marginBottom="8dp" 
     android:layout_marginRight="8dp" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toTopOf="parent" 
     android:layout_marginTop="8dp" 
     app:layout_constraintHorizontal_bias="0.666" 
     app:layout_constraintVertical_bias="1.0" 
     android:layout_marginStart="8dp" 
     android:layout_marginEnd="8dp" /> 


    <EditText 
     android:id="@+id/textNotAtHomes" 
     android:layout_width="346dp" 
     android:layout_height="477dp" 
     android:ems="10" 
     android:background="@color/hintTextColour" 
     android:inputType="textMultiLine" 
     android:hint="@string/hintText2" 
     android:textColorHint="@color/hintTextColour2" 
     android:layout_marginLeft="8dp" 
     android:gravity="fill_horizontal" 
     android:padding="6dp" 
     app:layout_constraintLeft_toLeftOf="parent" 
     android:layout_marginRight="8dp" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintBottom_toBottomOf="parent" 
     android:layout_marginBottom="8dp" 
     app:layout_constraintTop_toTopOf="parent" 
     android:layout_marginTop="8dp" 
     app:layout_constraintVertical_bias="0.303" /> 

    <TextView 
     android:id="@+id/ListName" 
     android:layout_width="344dp" 
     android:layout_height="37dp" 
     android:textColor="@color/blackText" 
     android:background="@color/whiteBackground" 
     android:layout_marginRight="8dp" 
     app:layout_constraintRight_toRightOf="parent" 
     android:layout_marginLeft="8dp" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintTop_toTopOf="parent" 
     android:layout_marginTop="8dp" 
     app:layout_constraintBottom_toBottomOf="parent" 
     android:layout_marginBottom="8dp" 
     app:layout_constraintHorizontal_bias="1.0" 
     app:layout_constraintVertical_bias="0.0" /> 


</android.support.constraint.ConstraintLayout> 

任何帮助表示赞赏。

+0

的可能的复制[如何发送从一个Android的活动到另一个使用意图的对象? ](http://stackoverflow.com/questions/2139134/how-to-send-an-object-from-one-android-activity-to-another-using-intents) – Lingeshwaran

回答

2

可以传递活动之间的数据这样

Intent intent = new Intent(activity2.this, activity1.class); 
intent.putExtra("message", message); 
startActivity(intent); 

,并与其他活动得到它

Bundle bundle = getIntent().getExtras(); 
String message = bundle.getString("message"); 
+0

@The抽象灯泡,因为你是新的。你需要确定你需要做的确切的事情。获取数据和显示对你来说是好的。但真正的问题是如何将数据从一个活动传递到另一个活动。 –

+0

谢谢你的男人。至少你真的试图帮助我解决我的问题。真的很感激它。 –

+0

@The抽象灯泡,如果这对你有用,你可以接受我的问题。 –

相关问题