简体   繁体   English

我无法在Visual Studio 2015(Android)中更改textView文本

[英]I cannot change my textView text in visual studio 2015 (Android)

I am very new to Visual Studio. 我是Visual Studio的新手。 I'm required to create an android app with Visual Studio. 我需要使用Visual Studio创建一个android应用。 I'm using Visual Studio 2015 Community. 我正在使用Visual Studio 2015社区。

Currently I have an android project with 2 activities (details and confirmation). 目前,我有2个活动(详细信息和确认)的android项目。 In the details page, there are EditText for the user to input the details and in the confirmation page, a TextView is used to show the user input. 在详细信息页面中,有EditText供用户输入详细信息,在确认页面中,TextView用于显示用户输入。

I have done this 2 activities, but when I run it, the confirmation page will be empty. 我已经完成了这2个活动,但是当我运行它时,确认页面将为空。 I'm not sure whether my code is right or not. 我不确定我的代码是否正确。

There are few problems I encounter right now :- 我现在遇到的问题很少:-

  • how to get the EditText value and assign it to variable 如何获取EditText值并将其分配给变量
  • how to pass the value from one activity to another 如何将价值从一项活动传递到另一项活动
  • how to read the value that is passed from the other activity 如何读取从其他活动传递的值
  • how to change the TextView text 如何更改 TextView文本

details.xml details.xml

<EditText
android:inputType="textMultiLine"
android:id="@+id/jobNumber"
android:layout_height="wrap_content" />

<EditText
android:inputType="textMultiLine"
android:id="@+id/statusCode"
android:layout_height="wrap_content" />

<EditText
android:inputType="textMultiLine"
android:id="@+id/statusDescription"
android:layout_height="wrap_content" />

<EditText
android:inputType="textMultiLine"
android:id="@+id/remarks"
android:layout_height="wrap_content" />

Details.cs Details.cs

protected override void OnCreate(Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState);

    SetContentView(Resource.Layout.DetailsPage);

    EditText jn = FindViewById<EditText>(Resource.Id.jobNumber);
    EditText sc = FindViewById<EditText>(Resource.Id.statusCode);
    EditText sd = FindViewById<EditText>(Resource.Id.statusDescription);
    EditText r = FindViewById<EditText>(Resource.Id.remarks);

    String jobNum = jn.Text;
    String statusC = sc.Text;
    String statusD = sd.Text;
    String remarks = r.Text;

    Button next = FindViewById<Button>(Resource.Id.next);

    next.Click += delegate
    {
        Intent act = new Intent(this, typeof(Confirmation));

        act.PutExtra("jNum", jobNum);
        act.PutExtra("SC", statusC);
        act.PutExtra("SD", statusD);
        act.PutExtra("R", remarks);

        StartActivity(act);
    };
}

confirmation.xml 确认文件

<TextView xmlns:tools="http://schemas.android.com/tools"    
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/textView7" />
<TextView
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/textView8" />
<TextView
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/textView9" />
<TextView
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/textView10" />

Confirmation.cs Confirmation.cs

protected override void OnCreate(Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState);

    SetContentView(Resource.Layout.Confirmation);

    String jobNumber = Intent.GetStringExtra("jNum");
    String statusCode = Intent.GetStringExtra("SC");
    String statusDescription = Intent.GetStringExtra("SD");
    String remarks = Intent.GetStringExtra("R");

    TextView jn = FindViewById<TextView>(Resource.Id.textView7b;
    TextView sc = FindViewById<TextView>(Resource.Id.textView8);
    TextView sd = FindViewById<TextView>(Resource.Id.textView9);
    TextView r = FindViewById<TextView>(Resource.Id.textView10);

    jn.Text = jobNumber;
    sc.Text = statusCode;
    sd.Text = statusDescription;
    r.Text = remarks;
}

Please help me, what do I do wrong? 请帮助我,我该怎么办? Why the TextView text does not change. 为什么TextView文本不会更改。 Thank you so much in advance for those who try and solve this problem for me. 非常感谢您为我解决这个问题的人。 APPRECIATE IT! 赞赏!

Regards, Azizi 问候,阿齐兹

You are trying to edit the Text variable directly, that is not possible. 您试图直接编辑Text变量,这是不可能的。 Use getText() and setText() instead. 请改用getText()和setText()。

For example: 例如:

Get text 取得文字

String jobNum = jn.getText() ; String jobNum = jn.getText() ;

Set text 设定文字

jn.setText(jobNumber)

Your error is that you are taken the texts when the Details activity starts, and obviously the text boxes are empty. 您的错误是,在“详细信息”活动开始时您被带走了文本,并且显然文本框为空。

Put this: 把这个:

String jobNum = jn.Text;
String statusC = sc.Text;
String statusD = sd.Text;
String remarks = r.Text;

Inside the button click delegate method. 在按钮内单击委托方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM