0

2 つの TextView から文字 (この場合は数字) を取得し、それらの一致する数字を使用して、一致した数字に基づいてアクションを実行する方法を知りたいですか?

たとえば、私のアクティビティには、以前のアクティビティから getIntent().getExtras を介して EditText 入力から提供された 2 つの形式のテキストがあり、このアクティビティでは、これらの 2 つのテキスト データに対して setText() を使用して、2 つの異なる TextView (TextView A と TextView A) に変換しています。 TextView B.TextView A に 5 があり、TextView B に 5 がある場合、一致する 5 に基づいてアクションを実行したい、または TextView A に 3 が 2 つあり、TextView B に 3 が 1 つしかない場合、どのように言いますか?その 1 つの一致と 1 つの不一致に基づいて実行し、アクションを実行できますか?

申し訳ありませんが、コードを投稿していません。私の活動でこれを達成したいだけですが、どこから始めればよいかわかりません。

更新:より良い例を示すためにコードを投稿しました。

package com.fullfrontalgames.numberfighter;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.urbanairship.UAirship;

public class Versus extends Activity {

    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        UAirship.shared().getAnalytics();
    }

    DBAdapter db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.versus);

        final DBAdapter db = new DBAdapter(this);
        db.open();

        Bundle PassedOverAttackNumbers = getIntent().getExtras();
        String AttackerNumbers = PassedOverAttackNumbers
                .getString("com.fullfrontalgames.numberfighter.ANumber2Pass");

        Bundle PassedOverDefenderNumbers = getIntent().getExtras();
        String DefenderNumbers = PassedOverDefenderNumbers
                .getString("com.fullfrontalgames.numberfighter.DNumber2Pass");

        Button Fight = (Button)findViewById(R.id.Fight);
        Button Continue = (Button)findViewById(R.id.Continue);
        ImageView AAvatar = (ImageView)findViewById(R.id.AttackerAvatar);
        ImageView DAvatar = (ImageView)findViewById(R.id.DefenderAvatar);
        TextView AName = (TextView)findViewById(R.id.AttackerName);
        TextView DName = (TextView)findViewById(R.id.DefenderName);
        TextView Admg = (TextView)findViewById(R.id.AttackerDamage);
        TextView Ddmg = (TextView)findViewById(R.id.DefenderDamage);
        TextView ANumbers = (TextView)findViewById(R.id.AttackerNumbers);
        TextView DNumbers = (TextView)findViewById(R.id.DefenderNumbers);
        TextView NBlock = (TextView)findViewById(R.id.NumbersBlocked);
        TextView NHit = (TextView)findViewById(R.id.NumbersHit);

        ANumbers.setText(AttackerNumbers);
        DNumbers.setText(DefenderNumbers);

        if (TextUtils.equals(AttackerNumbers, DefenderNumbers)) {
            NBlock.setText("1 Blocked");
            NBlock.setText("2 Blocked");
            NBlock.setText("3 Blocked");
            NBlock.setText("4 Blocked");
            NBlock.setText("5 Blocked");
            NBlock.setText("6 Blocked");
            NBlock.setText("7 Blocked");
            NBlock.setText("8 Blocked");
            NBlock.setText("9 Blocked");
        } else {
            NHit.setText("1 Landed");
            NHit.setText("2 Landed");
            NHit.setText("3 Landed");
            NHit.setText("4 Landed");
            NHit.setText("5 Landed");
            NHit.setText("6 Landed");
            NHit.setText("7 Landed");
            NHit.setText("8 Landed");
            NHit.setText("9 Landed");
        }

    }
}
4

2 に答える 2

1
public void findMatches(String A, String B, ArrayList<Character> matches, ArrayList<Character> unmatches)
{
    String D = B;
    int i = 0;
    while (i++ < 5)
    {
        char charToMatch = A.charAt(i);
        if (D.indexOf(charToMatch) == -1) 
        {
            unmatches.add(charToMatch);
        }
        else
        {
            matches.add(charToMatch);
            // replace the first occurrence of charToMatch with empty
            D = D.replaceFirst("[" + charToMatch + "]", "");
        }
    }
}  

上記の関数を使用するには、A、B、および 2 つのパスを渡し、たとえば ArrayList を初期化します。

ArrayList<Character> matches = new ArrayList<Character>();
ArrayList<Character> unmatches = new ArrayList<Character>();
findMatches(A, B, matches, unmatches);
于 2013-05-04T03:57:59.350 に答える
0

これのことですか?

String textA, textB;
if(textViewA.getText() != null) {
    textA = textViewA.getText().toString();
}
if(textViewB.getText() != null ) {
    textB = textViewB.getText().toString();
}
if(TextUtils.equals(textA, textB)) {
    //match, do somethings
} else {
    //no match, do somethings
}
于 2013-05-04T00:28:41.900 に答える