简体   繁体   English

如何在Xamarin android中放置循环加载动画

[英]How to put a circular loading animation in Xamarin android

My project is an android messenger app Using xamarin android. 我的项目是使用xamarin android的android Messenger应用程序。 I want to put a circular loading animation while on processing to send sms and disabled the button, and EditText. 我想在发送短信时禁用循环加载动画,并禁用按钮和EditText。 But I am totally newbie in android that's why I don't know how to do it. 但是我完全是android的新手,这就是为什么我不知道该怎么做的原因。 This is a running project. 这是一个正在运行的项目。 Please Advice me or give me some hint on how to do it. 请给我建议或给我一些提示。

//MainActivity.cs //MainActivity.cs

using Android.App;
using Android.OS;
using Android.Support.V7.App;
using Android.Runtime;
using Android.Widget;
using Android.Content.PM;
using Android.Telephony;
using System;
using Android;
using Android.Content;
using Android.Support.V4.Content;
using Android.Support.V4.App;
using Android.Util;
using Android.Support.Design.Widget;
using Android.Views;
using Android.Text;

namespace MhylesOrderingApp
{
    [Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
    public class MainActivity : AppCompatActivity, ActivityCompat.IOnRequestPermissionsResultCallback
    {
        static readonly int REQUEST_SENDSMS = 0;
        private SmsManager _smsManager;
        private BroadcastReceiver _smsSentBroadcastReceiver, _smsDeliveredBroadcastReceiver;
        View layout;
        private ProgressBar spinner;
        //public virtual ViewStates Visibility { get; set; }
        //SMSSentReceiver receiver;
        //private SmsManager smsManager;
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.activity_main);

            layout = FindViewById(Resource.Id.sample_main_layout);
            spinner = (ProgressBar)FindViewById(Resource.Id.progressBar1);
            var smsBtn = FindViewById<Button>(Resource.Id.btnSend);
            var phoneNum = FindViewById<EditText>(Resource.Id.phoneNum);
            var smsDate = FindViewById<EditText>(Resource.Id.txtDate);
            var smsCustomer = FindViewById<EditText>(Resource.Id.txtCustomer);
            var smsAddress = FindViewById<EditText>(Resource.Id.txtAddress);
            var smsCustNo = FindViewById<EditText>(Resource.Id.txtCustomerNo);
            var smsOrderProd = FindViewById<EditText>(Resource.Id.txtOrderedProduct);
            var smsProdQty = FindViewById<EditText>(Resource.Id.txtProductQty);
            var smsUnit = FindViewById<EditText>(Resource.Id.txtUnit);
            var smsAgentName = FindViewById<EditText>(Resource.Id.txtAgentName);
            spinner.Visibility = (ViewStates.Gone);

            _smsManager = SmsManager.Default;
            //receiver = new SMSSentReceiver();
            //smsManager = SmsManager.Default;
            //IntentFilter intentFilter = new IntentFilter("SMS_SENT");
            //intentFilter.AddAction("android.permission.SEND_SMS");

            ////RegisterReceiver(receiver, intentFilter);
            //smsBtn.Click += (s, e) =>
            //{
            //    var phone = phoneNum.Text;
            //    var message = sms.Text;

            //    var piSent = PendingIntent.GetBroadcast(this, 0, new Android.Content.Intent("SMS_SENT"), 0);
            //    //var piDelivered = PendingIntent.GetBroadcast(this, 0, new Android.Content.Intent("SMS_DELIVERED"), 0);

            //    smsManager.SendTextMessage(phone, null, message, piSent, null);
            //};
            smsBtn.Click += (s, e) =>
            {
                if (TextUtils.IsEmpty(smsDate.Text) || TextUtils.IsEmpty(phoneNum.Text) || TextUtils.IsEmpty(smsCustomer.Text)
                    || TextUtils.IsEmpty(smsAddress.Text) || TextUtils.IsEmpty(smsCustNo.Text) || TextUtils.IsEmpty(smsOrderProd.Text)
                    || TextUtils.IsEmpty(smsProdQty.Text) || TextUtils.IsEmpty(smsUnit.Text) || TextUtils.IsEmpty(smsAgentName.Text))
                {
                    Toast.MakeText(this, "Please fill out all the fields", ToastLength.Short).Show();
                    return;
                }
                else
                {
                    var phone = phoneNum.Text;
                    var message = smsDate.Text + "|" + smsCustomer.Text + "|" + smsAddress.Text + "|" + smsCustNo.Text
                                  + "|" + smsOrderProd.Text + "|" + smsProdQty.Text + "|" + smsUnit.Text + "|" + smsAgentName.Text;

                    var piSent = PendingIntent.GetBroadcast(this, 0, new Intent("SMS_SENT"), 0);
                    var piDelivered = PendingIntent.GetBroadcast(this, 0, new Intent("SMS_DELIVERED"), 0);

                    if ((int)Build.VERSION.SdkInt < 23)
                    {
                        spinner.Visibility = (ViewStates.Visible);
                        _smsManager.SendTextMessage(phone, null, message, piSent, piDelivered);
                        Toast.MakeText(this, "Sending Orders error! please restart the app", ToastLength.Short).Show();
                        return;
                    }
                    else
                    {
                        if (ActivityCompat.CheckSelfPermission(this, Manifest.Permission.SendSms) != (int)Permission.Granted)
                        {
                            // Permission is not granted. If necessary display rationale & request.
                            RequestSendSMSPermission();
                        }
                        else
                        {
                            // We have permission, go ahead and send SMS.
                            spinner.Visibility = (ViewStates.Visible);
                            _smsManager.SendTextMessage(phone, null, message, piSent, piDelivered);
                        }
                    }
                }
                //_smsManager.SendTextMessage(phone, null, message, piSent, piDelivered);
            };

        }
        public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
        {
            Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
            base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
        }
        public void RequestSendSMSPermission()
        {
            Log.Info("MainActivity", "Message permission has NOT been granted. Requesting permission.");
            if (ActivityCompat.ShouldShowRequestPermissionRationale(this, Manifest.Permission.SendSms))
            {
                Log.Info("MainActivity", "Displaying message permission rationale to provide additional context.");
                Snackbar.Make(layout, "Message permission is needed to send SMS.",
                    Snackbar.LengthIndefinite).SetAction("OK", new Action<View>(delegate (View obj) {
                        ActivityCompat.RequestPermissions(this, new String[] { Manifest.Permission.SendSms }, REQUEST_SENDSMS);
                    })).Show();
            }
            else
            {
                ActivityCompat.RequestPermissions(this, new String[] { Manifest.Permission.SendSms }, REQUEST_SENDSMS);
            }
        }

        //protected override void OnPause()
        //{
        //    base.OnPause();
        //    UnregisterReceiver(receiver);
        //}
        protected override void OnResume()
        {

            //spinner.Visibility = (ViewStates.Gone);
            base.OnResume();
            //spinner.setVisibility(View.GONE);
            _smsSentBroadcastReceiver = new SMSSentReceiver();
            _smsDeliveredBroadcastReceiver = new SMSDeliveredReceiver();

            RegisterReceiver(_smsSentBroadcastReceiver, new IntentFilter("SMS_SENT"));
            RegisterReceiver(_smsDeliveredBroadcastReceiver, new IntentFilter("SMS_DELIVERED"));
            //spinner.setVisibility(View.GONE);
        }

        protected override void OnPause()
        {
            base.OnPause();

            UnregisterReceiver(_smsSentBroadcastReceiver);
            UnregisterReceiver(_smsDeliveredBroadcastReceiver);
        }
    }
    //[BroadcastReceiver(Exported = true, Permission = "//receiver/@android:android.permission.SEND_SMS")]
    [BroadcastReceiver]
    public class SMSSentReceiver : BroadcastReceiver
    {
        public override void OnReceive(Context context, Intent intent)
        {
            switch ((int)ResultCode)
            {
                case (int)Result.Ok:
                    Toast.MakeText(Application.Context, "SMS has been sent", ToastLength.Short).Show();
                    break;
                case (int)SmsResultError.GenericFailure:
                    Toast.MakeText(Application.Context, "Generic Failure", ToastLength.Short).Show();
                    break;
                case (int)SmsResultError.NoService:
                    Toast.MakeText(Application.Context, "No Service", ToastLength.Short).Show();
                    break;
                case (int)SmsResultError.NullPdu:
                    Toast.MakeText(Application.Context, "Null PDU", ToastLength.Short).Show();
                    break;
                case (int)SmsResultError.RadioOff:
                    Toast.MakeText(Application.Context, "Radio Off", ToastLength.Short).Show();
                    break;
                default:
                    break;
            }
        }
    }
    //[BroadcastReceiver(Exported = true, Permission = "//receiver/@android:android.permission.SEND_SMS")]
    [BroadcastReceiver]
    public class SMSDeliveredReceiver : BroadcastReceiver
    {
        public override void OnReceive(Context context, Intent intent)
        {
            switch ((int)ResultCode)
            {
                case (int)Result.Ok:
                    Toast.MakeText(Application.Context, "SMS Delivered", ToastLength.Short).Show();
                    break;
                case (int)Result.Canceled:
                    Toast.MakeText(Application.Context, "SMS not delivered", ToastLength.Short).Show();
                    break;
            }
        }
    }
}

//activity_main.axml //activity_main.axml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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"
    android:orientation="vertical"
    android:id="@+id/sample_main_layout">
    <EditText
        android:id="@+id/phoneNum"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Recipient Number"/>
    <EditText
        android:id="@+id/txtDate"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="date"
        android:hint="Date"/>
    <EditText
        android:id="@+id/txtCustomer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Customer Name"/>
    <EditText
        android:id="@+id/txtAddress"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Customer Address"/>
    <EditText
        android:id="@+id/txtCustomerNo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Customer No."/>
    <EditText
        android:id="@+id/txtOrderedProduct"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Ordered Product Name"/>
    <EditText
        android:id="@+id/txtProductQty"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Quantity"/>
    <EditText
        android:id="@+id/txtUnit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Unit"/>
    <EditText
        android:id="@+id/txtAgentName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Agent Name"/>
    <Button
        android:id="@+id/btnSend"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Send Orders" />
    <ProgressBar
   android:id="@+id/progressBar1"
   style="?android:attr/progressBarStyleLarge"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_centerHorizontal="true" />
</LinearLayout>

If you want the circular progressbar, then use the android:indeterminate="true" attribute for the progressbar. 如果您需要圆形进度条,请为进度条使用android:indeterminate =“ true”属性。 Hope it helps :) 希望能帮助到你 :)

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

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