InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); //软键盘如果已经打开则关闭之 if (imm.isActive() == true) { imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS); }
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(et_server.getWindowToken(), 0);
import com.example.exmsimplewidget.R; import java.text.SimpleDateFormat; import java.util.Date; import android.app.Activity; import android.os.Bundle; import android.text.method.ScrollingMovementMethod; import android.view.Gravity; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class LiveTextActivity extends Activity implements OnClickListener { private TextView tv_live; private EditText et_news; private Button btn_publish; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_live_text); tv_live = (TextView) findViewById(R.id.tv_live); et_news = (EditText) findViewById(R.id.et_news); btn_publish = (Button) findViewById(R.id.btn_publish); btn_publish.setOnClickListener(this); tv_live.setGravity(Gravity.LEFT|Gravity.BOTTOM); tv_live.setLines(20); tv_live.setMaxLines(20); tv_live.setMovementMethod(new ScrollingMovementMethod()); } @Override public void onClick(View v) { if (v.getId() == R.id.btn_publish) { String desc = String.format("%s\n直播员 %s:%s", tv_live.getText(), getNowDateTime(), et_news.getText()); tv_live.setText(desc); } } private String getNowDateTime() { SimpleDateFormat s_format = new SimpleDateFormat("HH:mm:ss"); Date d_date = new Date(); String s_date = ""; s_date = s_format.format(d_date); return s_date; } }
import com.example.exmsimplewidget.R; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.text.Editable; import android.text.InputType; import android.text.Selection; import android.text.TextWatcher; import android.util.Log; import android.view.KeyEvent; import android.view.View; import android.view.View.OnKeyListener; import android.widget.Button; import android.widget.EditText; public class EnterInfoActivity extends Activity { private final static String TAG = "EnterInfoActivity"; private EditText et_user, et_password, et_job; private Button btn_cancel, btn_ok; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_enter_info); et_user = (EditText) findViewById(R.id.et_user); et_password = (EditText) findViewById(R.id.et_password); et_job = (EditText) findViewById(R.id.et_job); btn_cancel = (Button) findViewById(R.id.btn_cancel); btn_ok = (Button) findViewById(R.id.btn_ok); et_user.setInputType(InputType.TYPE_CLASS_TEXT); et_password.setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_PASSWORD); et_job.setInputType(InputType.TYPE_CLASS_TEXT); et_user.setOnKeyListener(new MyKeyListener(et_password, mUserRefresh)); et_password.addTextChangedListener(new MyTextWatcher(et_password, et_job)); et_job.addTextChangedListener(new MyTextWatcher(et_job, btn_ok)); } private final Handler mHandler = new Handler(); private Runnable mUserRefresh = new Runnable() { @Override public void run() { et_user.setText(et_user.getText().toString().replace("\r", "").replace("\n", "")); } }; class MyKeyListener implements OnKeyListener { private View mNextView = null; private Runnable mRun = null; public MyKeyListener(View vNext, Runnable run) { super(); if (vNext != null) { mNextView = vNext; } mRun = run; } @Override public boolean onKey(View v, int keyCode, KeyEvent event) { if(keyCode == 66 && mNextView != null) { mHandler.postDelayed(mRun, 50); mNextView.requestFocus(); if (mNextView instanceof EditText) { EditText et = (EditText)mNextView; et.setSelection(et.getText().length()); } } return false; } } class MyTextWatcher implements TextWatcher { private EditText mThisView = null; private View mNextView = null; public MyTextWatcher(EditText vThis, View vNext) { super(); mThisView = vThis; if (vNext != null) { mNextView = vNext; } } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { } @Override public void afterTextChanged(Editable s) { Log.d(TAG, "et_user.getText()="+et_user.getText()); String str = s.toString(); if (str.indexOf("\r") >= 0 || str.indexOf("\n") >= 0) { Log.d(TAG, "afterTextChanged mThisView.getId()="+mThisView.getId() +", mNextView.getId()="+mNextView.getId()); mThisView.setText(str.replace("\r", "").replace("\n", "")); if (mNextView != null) { mNextView.requestFocus(); if (mNextView instanceof EditText) { EditText et = (EditText)mNextView; Editable edit = et.getText(); Selection.setSelection(edit, edit.length()); } } } } } }