Reputation: 93
I want to start an activity through editText's text.
For example: When I enter any string, such as 'string', it should automatically start an activity.
Upvotes: 0
Views: 403
Reputation: 1102
This is NextActivity.java
public class NextActivity extends Activity {
//Your member variable declaration here
// Called when the activity is first created.
@Override
public void onCreate(Bundle savedInstanceState) {
//Your code here
}
}
After creation of a new Activity, we have to register it in file ‘AndroidManifest.xml’. For registering we have to create an entry in ‘AndroidManifest.xml’ as
**<activity android:name=".NextActivity" android:label="@string/app_name"/>**
Upvotes: -1
Reputation: 2037
You can use an OnkeyListener
myEditTextField.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_UP) {
if (myEditTextField.getText().toString().trim().equals("your string")) {
// start your activity
}
}
}
});
Upvotes: 0
Reputation: 7605
use this way
final EditText et = (EditText) findViewById(R.id.editText1);
et.addTextChangedListener(new TextWatcher()
{
public void afterTextChanged(Editable s){
}
}
public void beforeTextChanged(CharSequence s,int start,int count, int after){}
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(s.length() > 0) {
if(et.getText().toString().equals("string"){
Intent i=new Intent(YourActivity.this,SecondActivity.class);
startActivity(i);
}
}
});
Upvotes: 0
Reputation: 40416
I think using TextWatcher you can startActivity.You just check string in afterTextChanged
method...
if(s.toString().equals("string")){
//startActivity here
}
Like,
edittext.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
if(s.toString().equals("string")){
//startActivity here
}
}
});
Upvotes: 0
Reputation: 4258
You should use textwatcher event for achieving it. Because its call every time when user enter any thing in edittext.
I hope its helpful to you.
Upvotes: 1