0

私はAndroid開発に飛び込んでいます。私が関わったプロジェクトのほとんどは C# でした。デリゲートは、私が頻繁に使用した C# の要素であり、EventArgs を拡張するクラスを使用してデータを渡したり、set と get を使用してプロパティを渡したりするようなものも使用しました。プログラミングの知識があれば、かなりスムーズに Android 開発を始めることができると思います。問題は、Java で C# の delagte に似た実装メカニズムにアプローチする方法がまったくわからないことです。

以下に、C# で問題なく動作し、将来の Android プロジェクトで使用したい C# 言語のいくつかの要素を含むクラスの例を示します。誰かがこのコードの翻訳を私に提供できますか? 私は自分の例で作業し、その変換により、すべてをより速くキャッチできるようにしたいと考えています。また、トピックに関する貴重なリソース (デリゲートだけでなく、C# を Java に変換する一般的なトピック) も高く評価されます。

CountdownTimer.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SampleDelegateApp
{
    public class CountdownTimer
    {
        Timer tmrTicks = new Timer();
        int secondsLeft = 0;
        int numberOfSecondsToCountdown = 0;

        public bool IsWorking
        {
            get { return tmrTicks.Enabled; }
        }

        public CountdownTimer(int seconds)
        {
            if (secondsLeft < 0) secondsLeft = 0; 
            numberOfSecondsToCountdown = seconds;
            secondsLeft = seconds;

            tmrTicks.Interval = 1000;
            tmrTicks.Tick += new EventHandler(tmrTicks_Tick);
            tmrTicks.Enabled = false;
        }

        void tmrTicks_Tick(object sender, EventArgs e)
        {
            secondsLeft--;
            if (secondsLeft >= 1) 
                WhenCountdownTimerTick(new CountdownTimerEventArgs(secondsLeft, numberOfSecondsToCountdown, false));
            else
            {
                Stop();
                WhenCountdownTimerTick(new CountdownTimerEventArgs(secondsLeft, numberOfSecondsToCountdown, true));
            }
        }

        public void Reset()
        {
            Stop();
            secondsLeft = numberOfSecondsToCountdown;
            if (secondsLeft < 0) secondsLeft = 0;
            WhenCountdownTimerTick(new CountdownTimerEventArgs(secondsLeft, numberOfSecondsToCountdown, false));
        }

        public void Stop()
        {
            tmrTicks.Enabled = false;
        }

        public void Start()
        {
            if (secondsLeft <= 0)
            {
                secondsLeft = 0;
                WhenCountdownTimerTick(new CountdownTimerEventArgs(secondsLeft, numberOfSecondsToCountdown, true));
            }
            else
            {
                tmrTicks.Enabled = true;
            }
        }

        public delegate void CountdownTimerTickEventHandler(object sender, CountdownTimerEventArgs ea);

        public event CountdownTimerTickEventHandler CountdownTimerTick;

        protected virtual void WhenCountdownTimerTick(CountdownTimerEventArgs ea)
        {
            if (CountdownTimerTick != null)
            {
                CountdownTimerTick(this, ea);
            }
        }
    }

    public class CountdownTimerEventArgs : EventArgs
    {

        public string timeString = "";
        public float procentOfTimeLeft = 0.0f;
        public bool countdownFinished = false;

        public CountdownTimerEventArgs(int secondsLeft, int SecondsToCountdown, bool isfinished)
        {
            countdownFinished = isfinished;
            timeString = string.Format("{0:00}:{1:00}", secondsLeft / 60, secondsLeft % 60);
        }
    }
}

frmTest.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SampleDelegateApp
{
    public partial class frmTest : Form
    {
        CountdownTimer ctmrTest;

        public frmTest()
        {
            InitializeComponent();
            ctmrTest = new CountdownTimer(-44);
            ctmrTest.CountdownTimerTick += new CountdownTimer.CountdownTimerTickEventHandler(ctmrTest_CountdownTimerTick); 
        }

        void ctmrTest_CountdownTimerTick(object sender, CountdownTimerEventArgs ea)
        {
            lblTimer.Text = ea.timeString;
            if (ea.countdownFinished) countdownEnd();
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            ctmrTest.Reset();
            ctmrTest.Start();
        }

        void countdownEnd()
        {
            MessageBox.Show("Finish");
        }
    }
}
4

3 に答える 3

0

私はどうやら私の質問にいくつかの単語を使用したため、すべてが不明確になったり、私を助けようとした人がこの質問に関する私のニーズを完全に誤解したりしました。「デリゲート」という言葉をあちこちに置くと、さらに複雑になります。別の方法でもう1つの質問をした後、私は自分の質問に答えることができるようになりました。

Javaでc#イベントの動作を実現する方法(Android)

以下のコードが最高のコーディング標準に達していると言っているわけではありません。ここに示す例は実際のプロジェクトで使用されていますが、これはまさに私が探していたものです。以下の実例:

tester.java

public class tester{
    public static void main(String[] args) {
        test test1 = new test(); 
        test1.start();
    }
}

test.java

public class test implements CountdownTextTickListener {

    public test() { }

    public void start() {
        CountdownText ctx = new CountdownText(100);
        ctx.setListener(this);
        ctx.Start();
    }

    @Override
    public void CountdownTextTickEventFired(Object sender, 
                        CountdownTextTickEventArgs eventArgs) {
            System.out.println(eventArgs.TimeString);
            if(eventArgs.isStopped) System.out.println("- END -");
    }
}

CountdownTextTickListener.java

public interface CountdownTextTickListener {
     void CountdownTextTickEventFired(Object sender, CountdownTextTickEventArgs eventArgs);
}

CountdownTextTickEventArgs.java

public class CountdownTextTickEventArgs {

    public String TimeString = "";
    public boolean isStopped = false;

    public CountdownTextTickEventArgs(int seconds, boolean isStoppedState) {
        TimeString = String.format("%02d:%02d",seconds/60, seconds % 60);
        isStopped = isStoppedState;
    }

}

CountdownText.java

import java.util.Timer;
import java.util.TimerTask;

public class CountdownText {

    Timer tmrTicks = new Timer();
    int secondsLeft = 0;
    int numberOfSecondsToCountdown = 0;
    boolean isWorking = false;
    private CountdownTextTickListener listener = null;

    public boolean getIsWorking(){
        return isWorking;
    }

    public CountdownText(int seconds) {
        if (secondsLeft < 0) secondsLeft = 0; 
        numberOfSecondsToCountdown = seconds;
        secondsLeft = seconds;
    }

    void startTimer() {
        isWorking = true;
        fireEvent(secondsLeft, false);
        tmrTicks = new Timer();
        tmrTicks.scheduleAtFixedRate( new TimerTask(){
            @Override
            public void run(){
                tickTimer();
            }
        }, 1000, 1000); 
    }

    private void stopTimer() {
        isWorking = false;
        tmrTicks.cancel();
    }

    private void tickTimer() {
         secondsLeft--;
         if (secondsLeft >= 1) 
         {
             fireEvent(secondsLeft, false);
         }
         else
         {
             Stop();
             fireEvent(secondsLeft, true);
         }
    }

    public void Reset() {
        Stop();
        secondsLeft = numberOfSecondsToCountdown;
        fireEvent(secondsLeft, false);
    }

    public void Stop() {
        stopTimer();
    }

    public void Start() {
        if (secondsLeft <= 0)
        {
            secondsLeft = 0;
            fireEvent(secondsLeft, true);
        }
        else
        {
            startTimer();
        }
    }

    protected void fireEvent(int seconds, boolean isStoppedState) {
        if (listener != null) {
            Object sender = this; 
            CountdownTextTickEventArgs eventArgs = new CountdownTextTickEventArgs(seconds, isStoppedState);
            listener.CountdownTextTickEventFired(sender, eventArgs);
        }
    }

    public void setListener(CountdownTextTickListener listener) {
        this.listener = listener;
    }
}
于 2012-11-08T04:08:23.433 に答える
0

これは、観察者パターンに関するすべてです。.NET では、デリゲートなどを使用してこれを簡単に行うことができます。Java では、ハンドラーにインターフェイスを使用し、引数にプレーン オブジェクトを使用して、手動で実装する必要があります。調べてみると、たくさんの情報が見つかります。

于 2012-10-28T20:44:10.657 に答える
-1

私が理解していることから、C# コードを Android で動作させる方法は実際にはいくつかあります。状況によって C# で作業できる場合は、 Mono for Androidを検討することをお勧めします。コードを C# のままにしておくだけでなく、iOS 用の MonoTouch や Windows Phone 用の .NET 4.5 に簡単に移植することもできます。そうでなければ、私はあなたを助けることはできませんが、もっと知識のある人が助けてくれると確信しています.

于 2012-10-28T20:43:41.267 に答える