如何匹配两个TextViews中的字符

时间:2013-05-04 00:20:35

标签: java android eclipse

我想知道是否有可能以及如何从两个TextView中获取字符(在本例中为数字)并使用这些匹配的数字来根据匹配的数字执行操作?

例如,我在我的活动中通过getIntent()从前一个活动提供了两种形式的文本。来自EditText输入的getExtras,在这个活动中我将这两个文本数据上的setText()用于两个不同的TextViews( TextView A和TextView B)。现在假设如果TextView A有5并且TextView B有5,我想基于5获得匹配或者说TextView A有两个3并且TextView B只有一个3,我如何根据一场比赛和一场不匹配进行表演和采取行动?

抱歉,我没有张贴任何代码,只是在我的活动中我想完成这项工作,但我不知道从哪里开始。

更新:我发布了我的代码以提供更好的示例。

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");
        }

    }
}

2 个答案:

答案 0 :(得分: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和两个初始化ArrayList,例如

ArrayList<Character> matches = new ArrayList<Character>();
ArrayList<Character> unmatches = new ArrayList<Character>();
findMatches(A, B, matches, unmatches);

答案 1 :(得分: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
}