Android上的“使用完成”button单击button

好的在我的应用程序中,我有一个字段供用户input一个数字。 我把这个字段设置为只接受数字。 当用户点击该字段时,会popup键盘。 在键盘上(在ICS上)有一个完成button。 我想为键盘上的完成button来触发我的应用程序中的提交button。 我的代码如下。

package com.michaelpeerman.probability; import android.app.Activity; import android.app.ProgressDialog; import android.content.DialogInterface; import android.content.DialogInterface.OnCancelListener; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import java.util.Random; public class ProbabilityActivity extends Activity implements OnClickListener { private Button submit; ProgressDialog dialog; int increment; Thread background; int heads = 0; int tails = 0; public void onCreate(Bundle paramBundle) { super.onCreate(paramBundle); setContentView(R.layout.main); submit = ((Button) findViewById(R.id.submit)); submit.setOnClickListener(this); } public void onClick(View view) { increment = 1; dialog = new ProgressDialog(this); dialog.setCancelable(true); dialog.setMessage("Flipping Coin..."); dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); dialog.setProgress(0); EditText max = (EditText) findViewById(R.id.number); int maximum = Integer.parseInt(max.getText().toString()); dialog.setMax(maximum); dialog.show(); dialog.setOnCancelListener(new OnCancelListener(){ public void onCancel(DialogInterface dialog) { background.interrupt(); TextView result = (TextView) findViewById(R.id.result); result.setText("heads : " + heads + "\ntails : " + tails); }}); background = new Thread(new Runnable() { public void run() { heads=0; tails=0; for (int j = 0; !Thread.interrupted() && j < dialog.getMax(); j++) { int i = 1 + new Random().nextInt(2); if (i == 1) heads++; if (i == 2) tails++; progressHandler.sendMessage(progressHandler.obtainMessage()); } } }); background.start(); } Handler progressHandler = new Handler() { public void handleMessage(Message msg) { dialog.incrementProgressBy(increment); if (dialog.getProgress() == dialog.getMax()) { dialog.dismiss(); TextView result = (TextView) findViewById(R.id.result); result.setText("heads : " + heads + "\ntails : " + tails); } } }; } 

你也可以使用这个(在EditText上执行动作时设置一个特殊的侦听器),它对DONE和RETURN都起作用:

 max.setOnEditorActionListener(new OnEditorActionListener() { public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) || (actionId == EditorInfo.IME_ACTION_DONE)) { Log.i(TAG,"Enter pressed"); } return false; } }); 

尝试这个:

 max.setOnKeyListener(new OnKeyListener(){ @Override public boolean onKey(View v, int keyCode, KeyEvent event){ if(keyCode == event.KEYCODE_ENTER){ //do what you want } } }); 

您可以尝试使用IME_ACTION_DONE

此操作执行“完成”操作,无需input,IME将被closures。

  Your_EditTextObj.setOnEditorActionListener(new TextView.OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { boolean handled = false; if (actionId == EditorInfo.IME_ACTION_DONE) { /* Write your logic here that will be executed when user taps next button */ handled = true; } return handled; } }); 

试试这个Xamarin.Android(跨平台)

 edittext.EditorAction += (object sender, TextView.EditorActionEventArgs e) { if (e.ActionId.Equals (global::Android.Views.InputMethods.ImeAction.Done)) { //TODO Something } }; 

当您创build一个LoginActivity时,我从AndroidStudio中复制了以下代码。 我使用ime属性

在你的布局

 <EditText android:id="@+id/unidades" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/prompt_unidades" android:inputType="number" android:maxLines="1" android:singleLine="true" android:textAppearance="?android:textAppearanceSmall" android:enabled="true" android:focusable="true" android:gravity="right" android:imeActionId="@+id/cantidad" android:imeActionLabel="@string/add" android:imeOptions="actionUnspecified"/> 

在你的活动

 editTextUnidades.setOnEditorActionListener(new TextView.OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if (actionId == R.id.cantidad || actionId == EditorInfo.IME_NULL) { addDetalle(null); return true; } return false; } }); 

你可以在关键的监听器上实现:

 public class ProbabilityActivity extends Activity implements OnClickListener, View.OnKeyListener { 

在onCreate:

 max.setOnKeyListener(this); 

 @Override public boolean onKey(View v, int keyCode, KeyEvent event){ if(keyCode == event.KEYCODE_ENTER){ //call your button method here } return true; } 

在你的布局中使用这个类:

 public class ActionEditText extends EditText { public ActionEditText(Context context) { super(context); } public ActionEditText(Context context, AttributeSet attrs) { super(context, attrs); } public ActionEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override public InputConnection onCreateInputConnection(EditorInfo outAttrs) { InputConnection conn = super.onCreateInputConnection(outAttrs); outAttrs.imeOptions &= ~EditorInfo.IME_FLAG_NO_ENTER_ACTION; return conn; } 

}

在xml中:

 <com.test.custom.ActionEditText android:id="@+id/postED" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:background="@android:color/transparent" android:gravity="top|left" android:hint="@string/msg_type_message_here" android:imeOptions="actionSend" android:inputType="textMultiLine" android:maxLines="5" android:padding="5dip" android:scrollbarAlwaysDrawVerticalTrack="true" android:textColor="@color/white" android:textSize="20sp" /> 
 max.setOnKeyListener(new OnKeyListener(){ @Override public boolean onKey(View v, int keyCode, KeyEvent event){ if(keyCode == event.KEYCODE_ENTER){ //do what you want } } }); 

你最后的Edittext .setOnEditorActionListener调用这个方法自动命中api

我在et_password中的LoginActivity中调用

  et_Pass.setOnEditorActionListener(new TextView.OnEditorActionListener() { public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) || (actionId == EditorInfo.IME_ACTION_DONE)) { Log.i(TAG,"Enter pressed"); Log.i(Check Internet," and Connect To Server"); } return false; } }); 

正常工作