英文:
How to use EditText to go another activity in Android Studio
问题
在Android Studio的一个活动中,我想使用EditText输入来跳转到第二个活动,我的代码能够工作,但我必须按两次才能实现,如何更正为只需按一次
editText.setInputType(InputType.TYPE_NULL);
editText.setShowSoftInputOnFocus(false);
public void addListenerOnButton() {
editText.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String text = editText.getText().toString();
Intent myIntent = new Intent(view.getContext(), Calculated.class);
myIntent.putExtra("mytext", text);
startActivity(myIntent);
}
});
}
英文:
In an Android studio activity I want to use the EditText input to go to a second activity, my code works, but I have to press twice for this to work, how can I correct this to only press once
editText.setInputType(InputType.TYPE_NULL);
editText.setShowSoftInputOnFocus(false);
public void addListenerOnButton() {
editText.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String text = editText.getText().toString();
Intent myIntent = new Intent(view.getContext(),Calculated.class);
myIntent.putExtra("mytext",text);
startActivity(myIntent);
}
});
}
答案1
得分: 1
为什么要将EditText
的监听器包装在另一个监听器中?<br/>
更改为以下方式:
editText.setInputType(InputType.TYPE_NULL);
editText.setShowSoftInputOnFocus(false);
editText.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String text = editText.getText().toString();
Intent myIntent = new Intent(view.getContext(), Calculated.class);
myIntent.putExtra("mytext", text);
startActivity(myIntent);
}
});
这样,当您单击EditText
时,将启动另一个活动。<br/>
如果您想要单击Button
来启动活动,并且有一个带有ID button
的按钮,则在Button
上设置监听器:
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String text = editText.getText().toString();
Intent myIntent = new Intent(view.getContext(), Calculated.class);
myIntent.putExtra("mytext", text);
startActivity(myIntent);
}
});
英文:
Why did you wrap the listener of the EditText
inside another listener?<br/>
Change to this:
editText.setInputType(InputType.TYPE_NULL);
editText.setShowSoftInputOnFocus(false);
editText.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String text = editText.getText().toString();
Intent myIntent = new Intent(view.getContext(),Calculated.class);
myIntent.putExtra("mytext",text);
startActivity(myIntent);
}
});
this way when you click on the EditText
you will start the other activity.<br/>
If you want to click a Button
to start the activity, say with id button
, then set the listener on the Button
:
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String text = editText.getText().toString();
Intent myIntent = new Intent(view.getContext(),Calculated.class);
myIntent.putExtra("mytext",text);
startActivity(myIntent);
}
});
答案2
得分: 1
什么意思使用EditText输入来转到第二个活动?
如果您想要点击逻辑,请使用按钮。
如果您想要使用EditText的“完成”按钮:
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
if (i == EditorInfo.IME_ACTION_DONE) {
String text = editText.getText().toString();
Intent myIntent = new Intent(view.getContext(), Calculated.class);
myIntent.putExtra("mytext", text);
startActivity(myIntent);
return true;
}
return false;
}
});
在您的layout.xml中,在您的editText中添加:
android:inputType="text"
android:imeOptions="actionDone"
英文:
What do you mean by using the EditText input to go to a second activity ?
If you want a click logic, use a Button.
If you want to use the 'DONE' button of the EditText:
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
if (i == EditorInfo.IME_ACTION_DONE) {
String text = editText.getText().toString();
Intent myIntent = new Intent(view.getContext(),Calculated.class);
myIntent.putExtra("mytext",text);
startActivity(myIntent);
return true;
}
return false;
}
});
On your layout.xml, on your editText you need to add:
android:inputType="text"
android:imeOptions="actionDone"
答案3
得分: 1
我认为,你应该使用Button,而不是EditText,但是如果你仍然想要的话;
Button btnClick;
btnClick = (Button) findViewById(R.id.btnClickActivity);
btnClick.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
});
这样就可以了。
英文:
I think, you should use Button instead of Edittext but still if you want;
EditText edtClick;
edtClick = (EditText) findViewById(R.id.edtClickActivity);
edtClick.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
});
That works.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论