docs.microsoft的Xamarin教程,其中有一些补充内容

时间:2018-10-16 08:07:24

标签: c# android xamarin

您好,我正在尝试使用Xamarin制作我的第一个AndroidApp。我使用了https://docs.microsoft.com/en-us/xamarin/android/get-started/hello-android-multiscreen/hello-android-multiscreen-quickstart?pivots=windows链接中的教程,但是我没有使用将字母转换为数字的方法,而是使用了一种简单的方法来从输入字段中反转字符串。

我的activity_main.axml具有以下控件:

<TextView
    android:text="Input Text:"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textView1" />
<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/textView1"
    android:id="@+id/TextToReverse" />
<Button
    android:text="Reverse"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/TextToReverse"
    android:id="@+id/ReverseBtn" />
<TextView
    android:layout_width="match_parent"
    android:layout_height="34.5dp"
    android:layout_below="@id/ReverseBtn"
    android:id="@+id/ReversedText" />
<Button
    android:text="History"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/ReversedText"
    android:id="@+id/HistoryBtn"
    android:enabled="false" />  

我的MainActivity.cs

base.OnCreate(savedInstanceState);
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.activity_main);

            List<string> reversedStringsMain = new List<string>();

            //find UI elements by id
            EditText textToRev = FindViewById<EditText>(Resource.Id.TextToReverse);
            TextView reversedText = FindViewById<TextView>(Resource.Id.ReversedText);
            Button reverseBtn = FindViewById<Button>(Resource.Id.ReverseBtn);
            Button historyBtn = FindViewById<Button>(Resource.Id.HistoryBtn);

            reverseBtn.Click += (sender, e) => 
            {
                string output = ReverseMethod.Reverse(textToRev.Text);
                if (string.IsNullOrWhiteSpace(output))
                {
                    reversedText.Text = string.Empty;
                }
                else
                {
                    reversedText.Text = output;
                    reversedStringsMain.Add(textToRev.Text);
                    historyBtn.Enabled = true;
                }

            };

            historyBtn.Click += (sender, e) => 
            {
                var intent = new Intent(this, typeof(History));
                intent.PutStringArrayListExtra("reversed_strings", reversedStringsMain);
                StartActivity(intent);
            };  

所以在这里,我有一个数组来保存我已经反转过的所有字符串,还有一个历史按钮事件,它显示反转后的字符串列表。

我用以下代码创建了一个新的ListActivity:

 base.OnCreate(savedInstanceState);

            // Create your application here
            var reversedStrings = Intent.Extras.GetStringArrayList("reversed_strings") ?? new string[0];
            this.ListAdapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, reversedStrings);


            ListView.ItemClick += delegate (object sender, AdapterView.ItemClickEventArgs args)
            {
                //code that I don't know how to make
                EditText textToReverse = FindViewById<EditText>(Resource.Id.TextToReverse);
                textToReverse.Text =  MainActivity.reversedStringsMain[args.Position]; //here I get "Object reference not set to an instance of an object."                       

            };  

我的目标是使列表中的项目可单击,当我单击它们时,我希望从主活动提交的输入获得我单击的项目的值。

我还想在第二个屏幕上添加一个按钮,以清除整个列表。

1 个答案:

答案 0 :(得分:0)

要将某些数据从HistoryActivity传递回MainActivity,您不应直接引用MainActivity或将数据保存在静态变量中,而是

使用StartActivityForResult方法启动HistoryActivity

historyBtn.Click += (sender, e) => 
{
    var intent = new Intent(this, typeof(History));
    intent.PutStringArrayListExtra("reversed_strings", reversedStringsMain);
    StartActivityForResult(intent, 2);
};

FinishHistoryActivity的结果:

ListView.ItemClick += delegate (object sender, AdapterView.ItemClickEventArgs args)
{
    Intent intent = new Intent(this, typeof(HistoryActivity)); 
    intent.PutExtra("position", args.Position); 
    SetResult(Result.Ok, intent); 
    Finish();
} 

覆盖MainActivity中的OnActivityResult,以从传递的意图中读取数据:

protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
    base.OnActivityResult(requestCode, resultCode, data);

    switch (resultCode)
    {
        case Result.Ok:
            var position = data.getIntExtra("position", 0);
            var selectedString = reversedStringsMain[position];
            // fill in input field in MainActivity
            break;
    }
}