0
public class MainActivity extends Activity implements OnClickListener {

    Button btn,btn1;
    String n,m;
    EditText t1,t2;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        t1 = (EditText) findViewById(R.id.editText1);
        t2 = (EditText) findViewById(R.id.editText2);

        n= t1.getText().toString();
        m = t2.getText().toString();
        btn = (Button) findViewById(R.id.button1);
        btn.setOnClickListener(this);

        btn1 = (Button) findViewById(R.id.button2);
        btn1.setOnClickListener(this);

    }
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        switch (v.getId()) {
            case R.id.button1:
                if(n.equalsIgnoreCase("subbu")&&m.equalsIgnoreCase("12345")){
                    Toast.makeText(this, "WELCOME",Toast.LENGTH_LONG).show();
                }
                break;

            default:
                if(n.equalsIgnoreCase("pani")&&m.equalsIgnoreCase("12345")){
                    Toast.makeText(this, "Hi subbu",Toast.LENGTH_LONG).show();
                    MediaPlayer myplayer;

                    myplayer = MediaPlayer.create(this, R.raw.alimba);
                    myplayer.start();
                }
                break;
        }

    }

}

我正在使用上面的代码,但没有得到输出我是初学者我想获得editText的文本并想与if语句中的其他文本进行比较谁能帮我做?

4

1 回答 1

0

问题是您只获取开始时的文本,t1而不是单击按钮时的文本。t2Activity

将以下两行移到public void onClick(View v) {.

n = t1.getText().toString();
m = t2.getText().toString();
于 2012-09-09T05:31:27.163 回答