Java 如何为textview和button实现相同的操作?
我希望在文本编辑(s)中按下按钮时发生相同的事情 以下是用于文本编辑的Java 如何为textview和button实现相同的操作?,java,android,handler,Java,Android,Handler,我希望在文本编辑(s)中按下按钮时发生相同的事情 以下是用于文本编辑的OnEditorActionListener的实现: private OnEditorActionListener clickedEnter = (new OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { //implemtation
OnEditorActionListener
的实现:
private OnEditorActionListener clickedEnter = (new OnEditorActionListener()
{
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
//implemtation of task1
}
return false;
}
});
下面是按钮的实现:
show.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
//How can I call clickedEnter here?
}
});
原因是,我希望代码更少,调试更容易等等。提前感谢。您可以创建一个方法并在两个函数中调用它,或者在第一个方法中调用show.performClick() 解决方案1:
private OnEditorActionListener clickedEnter = (new OnEditorActionListener()
{
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
yourMethod();
}
return false;
}
});
show.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
yourMethod();
}
});
private void yourMethod(){
//Instructions
}
解决方案2:
private Button show;
private OnEditorActionListener clickedEnter = (new OnEditorActionListener()
{
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
show.performClick();
}
return false;
}
});
show.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
//Instructions...
}
});
您可以创建一个方法并在两个函数中调用它,也可以在第一个方法中调用show.performClick() 解决方案1:
private OnEditorActionListener clickedEnter = (new OnEditorActionListener()
{
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
yourMethod();
}
return false;
}
});
show.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
yourMethod();
}
});
private void yourMethod(){
//Instructions
}
解决方案2:
private Button show;
private OnEditorActionListener clickedEnter = (new OnEditorActionListener()
{
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
show.performClick();
}
return false;
}
});
show.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
//Instructions...
}
});
您可以创建一个方法并在两个函数中调用它,也可以在第一个方法中调用show.performClick() 解决方案1:
private OnEditorActionListener clickedEnter = (new OnEditorActionListener()
{
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
yourMethod();
}
return false;
}
});
show.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
yourMethod();
}
});
private void yourMethod(){
//Instructions
}
解决方案2:
private Button show;
private OnEditorActionListener clickedEnter = (new OnEditorActionListener()
{
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
show.performClick();
}
return false;
}
});
show.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
//Instructions...
}
});
您可以创建一个方法并在两个函数中调用它,也可以在第一个方法中调用show.performClick() 解决方案1:
private OnEditorActionListener clickedEnter = (new OnEditorActionListener()
{
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
yourMethod();
}
return false;
}
});
show.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
yourMethod();
}
});
private void yourMethod(){
//Instructions
}
解决方案2:
private Button show;
private OnEditorActionListener clickedEnter = (new OnEditorActionListener()
{
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
show.performClick();
}
return false;
}
});
show.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
//Instructions...
}
});
编辑:
下面是我的xml:
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="200dp "
android:text="Done!"/>
<EditText
android:id="@+id/textView"
android:inputType="text"
android:singleLine="true"
android:imeOptions="actionDone"
android:textSize="30dp"
android:textColor="#333"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:hint="Enter text"/>
取决于要在编辑器中调用的内容。编辑:
下面是我的xml:
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="200dp "
android:text="Done!"/>
<EditText
android:id="@+id/textView"
android:inputType="text"
android:singleLine="true"
android:imeOptions="actionDone"
android:textSize="30dp"
android:textColor="#333"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:hint="Enter text"/>
取决于要在编辑器中调用的内容。编辑:
下面是我的xml:
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="200dp "
android:text="Done!"/>
<EditText
android:id="@+id/textView"
android:inputType="text"
android:singleLine="true"
android:imeOptions="actionDone"
android:textSize="30dp"
android:textColor="#333"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:hint="Enter text"/>
取决于要在编辑器中调用的内容。编辑:
下面是我的xml:
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="200dp "
android:text="Done!"/>
<EditText
android:id="@+id/textView"
android:inputType="text"
android:singleLine="true"
android:imeOptions="actionDone"
android:textSize="30dp"
android:textColor="#333"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:hint="Enter text"/>
取决于要在编辑器中调用的内容。您可以为任务创建不同的方法,并在两个侦听器事件中调用该方法。您可以为任务创建不同的方法,并在两个侦听器事件中调用该方法。您可以为任务创建不同的方法,并在两个侦听器事件中调用该方法监听器的事件。您可以为您的任务创建一个不同的方法,并在两个监听器的事件中调用该方法。我想您需要这样的方法
etOne.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
bSearch.performClick();
closeKeyboard(getActivity(),
etOne.getWindowToken());
return true;
}
return false;
}
});
及
现在按钮将//执行某些操作。。如果用户在键盘上按enter键时需要这样做,则会发生与该按钮相同的情况。。只需调用bSearch.performClick()代码>希望对您有所帮助
编辑:也关闭键盘(getActivity(),etOne.getWindowToken())按enter键后,code>将自动隐藏软键盘。我想您需要这样的功能
etOne.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
bSearch.performClick();
closeKeyboard(getActivity(),
etOne.getWindowToken());
return true;
}
return false;
}
});
及
现在按钮将//执行某些操作。。如果用户在键盘上按enter键时需要这样做,则会发生与该按钮相同的情况。。只需调用bSearch.performClick()代码>希望对您有所帮助
编辑:也关闭键盘(getActivity(),etOne.getWindowToken())按enter键后,code>将自动隐藏软键盘。我想您需要这样的功能
etOne.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
bSearch.performClick();
closeKeyboard(getActivity(),
etOne.getWindowToken());
return true;
}
return false;
}
});
及
现在按钮将//执行某些操作。。如果用户在键盘上按enter键时需要这样做,则会发生与该按钮相同的情况。。只需调用bSearch.performClick()代码>希望对您有所帮助
编辑:也关闭键盘(getActivity(),etOne.getWindowToken())按enter键后,code>将自动隐藏软键盘。我想您需要这样的功能
etOne.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
bSearch.performClick();
closeKeyboard(getActivity(),
etOne.getWindowToken());
return true;
}
return false;
}
});
及
现在按钮将//执行某些操作。。如果用户在键盘上按enter键时需要这样做,则会发生与该按钮相同的情况。。只需调用bSearch.performClick()代码>希望对您有所帮助
编辑:也关闭键盘(getActivity(),etOne.getWindowToken())
将在按下enter键后自动隐藏软键盘。创建一个包含“task1”的函数,您希望从两个侦听器执行并调用该函数。简单!创建一个包含要执行的“task1”的函数,并从两个侦听器调用该函数。简单!创建一个包含要执行的“task1”的函数,并从两个侦听器调用该函数。简单!创建一个包含要执行的“task1”的函数,并从两个侦听器调用该函数。简单!感谢您的回答,但是我如何在第二个解决方案中调用Clickedeter?再次感谢,但是在第二个解决方案中,我需要在第一个解决方案中有说明,在第二个解决方案中我调用第一个。performClick()仅适用于View,因此您将无法在第二个侦听器中调用此函数。但是,如果您使用第二种解决方案,它应该仍然有效,因为您将执行相同的指令。如果这不起作用,您仍然可以使用第一个解决方案。感谢您的回答,但是我如何在第二个解决方案中调用Clickedeter?再次感谢,但在第二个解决方案中,我需要在第一个解决方案中有说明,在第二个解决方案中我调用第一个。performClick()仅适用于查看,因此,您将无法在第二个侦听器中调用此函数。但是,如果您使用第二种解决方案,它应该仍然有效,因为您将执行相同的指令。如果这不起作用,您仍然可以使用第一个解决方案。感谢您的回答,但是我如何在第二个解决方案中调用Clickedeter?再次感谢,但在第二个解决方案中,我需要在第一个解决方案中有说明,在第二个解决方案中我调用第一个。performClick()仅适用于查看,因此,您将无法在第二个侦听器中调用此函数。但是,如果您使用第二种解决方案,它应该仍然有效,因为您将执行相同的指令。如果这不起作用,您仍然可以使用第一个解决方案。感谢您的回答,但是我如何在第二个解决方案中调用Clickedeter?再次感谢,但在第二个解决方案中,我需要在第一个解决方案中有说明,在第二个解决方案中我调用第一个。performClick()仅适用于查看,因此,您将无法在第二个侦听器中调用此函数。但是,如果您使用第二种解决方案,它应该仍然有效,因为您将执行相同的指令。如果这不起作用,您仍然可以使用第一个解决方案。我需要调用Clickedeter。我真的不确定我需要换哪一个@伯纳德,我的解决方案有什么问题?它是干净的