3

一連のアクションを実行する onTouch メソッドを持つカスタム ビュー (DrawFigures) があります。カスタム ビューを動的に追加するためのいくつかのボタン、TextView および RelativeView を含むレイアウトがあります。次に、MainActivity.class で、このカスタム ビューで onTouch メソッドが呼び出されたときに、DrawFigures のいくつかの属性の値を受け取り、それらを TextView コンポーネントに表示する必要があります。

カスタム ビューのコードは次のとおりです。

public abstract class DrawFigures extends View{
    private float value1;
    public boolean onTouch(Motion Event){
        int action = event.getAction();
        if(action==MotionEvent.ACTION_MOVE){
            // ... methods that provoke that value1 changes
        }else if(action==MotionEvent.ACTION_DOWN){
            // ...        
        }else if(action==MotionEvent.ACTION_UP){
            // ...
        }
    }
    // ...
    return true;
}

DrawFigures の onTouch メソッドが原因で、値が変化している間、MainActivity の TextView に表示する value1 を受け取ることができませんでした。この問題はどのように解決できますか? MianActivity に OnTouch メソッドを実装しますか? どうやってするの?MainActivity.class は次のようになります。

public class MainActivity extends Activity implements View.OnTouchListener{
    DrawFigures fig;
    TextView txtInfo;
    // ...
    public void btnSecRec(View v){
        int id = v.getId();
        if (id == R.id.btnSecRec){
            RelativeLayout parent = (RelativeLayout)findViewById(R.id.viewDatosSec);
            parent.removeAllViews();
            fig = new DrawRectangleWithDimensions(this);
            fig.setOnTouchListener(this);
            fig.setFigure(10, 40);
            parent.addView(fig);
        }
    }
    public boolean onTouch(View v, MotionEvent event) {
        int action = event.getAction();
        if (action == MotionEvent.ACTION_MOVE) {

            if (fig.touching == true) {
                String value = Float.toString(fig.getChangingDimensionValue());

                txtInfo.setText(value);
            }
        }
        return true;
    }
}
4

3 に答える 3

0

このように、非アクティビティ クラスで value1 を static として宣言できます。

static float value1;

次に、次のように Activity クラスで値を取得します。

public boolean onTouch(View v, MotionEvent event) {
    int action = event.getAction();
    String value = Double.toString(DrawFigures.value1);
    if (action == MotionEvent.ACTION_MOVE) {

        if (fig.touching == true) {
            String value = Float.toString(fig.getChangingDimensionValue());

            txtInfoc.setText(value);
        }
    }

または、値が非公開であるため、メソッドを使用settergetterて DrawFigures のオブジェクトで値を設定および取得し、アプリ全体で使用することができます。

値を永続化したい場合は、SharedPreferencesを使用できます。

于 2015-02-20T11:46:25.550 に答える
0

しばらく試した後、私は解決策を見つけました。MainActivity の onTouch メソッド内のインスタンス「fig」を使用して、DrawFigures クラスの onTouchEvent メソッドを呼び出すだけです。

時間を割いて助けてくれた人々に感謝したいです、ありがとう!

コードは次のとおりです。

public class MainActivity extends Activity implements View.OnTouchListener{
DrawFigures fig;
TextView txtInfo;
// ...
public void btnSecRec(View v){
    int id = v.getId();
    if (id == R.id.btnSecRec){
        RelativeLayout parent = (RelativeLayout)findViewById(R.id.viewDatosSec);
        parent.removeAllViews();
        fig = new DrawRectangleWithDimensions(this);
        fig.setOnTouchListener(this);
        fig.setFigure(10, 40);
        parent.addView(fig);
    }
}
public boolean onTouch(View v, MotionEvent event) {
    int action = event.getAction();
    fig.onTouchEvent(event);
    if (action == MotionEvent.ACTION_MOVE) {

        if (fig.touching == true) {
            String value = Float.toString(fig.getChangingDimensionValue());
            txtInfo.setText(value);
        }
    }
    return true;
}

}

于 2015-02-21T17:02:14.367 に答える