全部博文(135)
2010年(135)
分类: LINUX
2010-04-09 12:00:40
import android.app.Activity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.LinearLayout;
public class test extends Activity implements OnClickListener {
Button button;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout me = new LinearLayout(this);
LinearLayout.LayoutParams my = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
button = new Button(this);
button.setId(123);
button.setText("ok");
button.setOnClickListener((android.view.View.OnClickListener) this);
me.addView(button, my);
this.setContentView(me);
}
public void onClick(View v) {
InputMethodManager m = (InputMethodManager) button.getContext()
.getSystemService(INPUT_METHOD_SERVICE);
m.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
}
@Override
public void onClick(DialogInterface dialog, int which) {
}
}
((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(WidgetSearchActivity.this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
//显示软键盘,控件可以是EditText,TextView ((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE)).showSoftInput(控件, 0); |
2.利用时间来控制软键盘的弹出实例:
public class MainActivity extends Activity {
private Context context;
private EditText mEditText;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
context = this;
mEditText = (EditText) findViewById(R.id.EditText01);
mEditText.requestFocus();
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
InputMethodManager m = (InputMethodManager) context.getSystemService (Context.INPUT_METHOD_SERVICE);
m.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
}
}, 1000);
}
}