Click here to Skip to main content
15,883,828 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i'm beginner in Android development. the problem is i can't open the next activity after compare string .
Java
package com.e.search;



import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

	Button btnSearch;
	EditText keywords;
	String word;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        word = "Hello";
        btnSearch = (Button) findViewById(R.id.btnSearch);
        keywords = (EditText) findViewById(R.id.editTextKeyWords);
        
        btnSearch.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				
				if (keywords.getText().equals("")){
					Intent intent = new Intent(v.getContext(), Second.class);
					startActivity(intent);
				}else if(keywords.getText().equals("a")){
					Intent intent = new Intent(v.getContext(), Thrid.class);
					startActivity(intent);
					
				}
				
			}
        	
        });
        
        
        
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
}
Posted
Updated 7-May-15 4:18am
v2

add
Java
toString()

Java
if (keywords.getText().toString().equals("")){
    Intent intent = new Intent(v.getContext(), Second.class);
    startActivity(intent);
}else if(keywords.getText().toString().equals("a")){
    Intent intent = new Intent(v.getContext(), Thrid.class);
    startActivity(intent);
 
Share this answer
 
hello . at first i would suggest debugging your project. it helps you to find your problem but if you dont want to do it you can use this piece of code which shows you how to act .

java code :
Java
public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);	
	}
	public void method1(View v){
		String Word="a";
		String etValue="";
		EditText et=(EditText)findViewById(R.id.et1);
		etValue=et.getText().toString();
		if(etValue.equals("a")){
			// here you can   do any action you want to do
			// in your case you have to use intents
			Toast.makeText(this, "it works fine", Toast.LENGTH_LONG).show();
		}
		else{
			Toast.makeText(this, "insert the correct word ! ", Toast.LENGTH_LONG).show();
		}		
	}
}
 
Share this answer
 
v5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900