0

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

例如,我在我的活动中通过 getIntent().getExtras 从上一个活动提供了两种形式的文本,来自 EditText 输入,在这个活动中,我在这两个文本数据上使用 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");
        }

    }
}
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 和两个初始化 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 回答