1.单项选择列表框
转自:http://blog.csdn.net/xys289187120/article/details/6601613
lertDialog.Builder builder = new AlertDialog.Builder(MainDialog.this);
mSingleChoiceID = -1;
builder.setIcon(R.drawable.icon);
builder.setTitle("单项选择");
builder.setSingleChoiceItems(mItems, 0, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
mSingleChoiceID = whichButton;
showDialog("你选择的id为" + whichButton + " , " + mItems[whichButton]);
}
});
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
if(mSingleChoiceID > 0) {
showDialog("你选择的是" + mSingleChoiceID);
}
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
});
builder.create().show();
2.多项选择列表框
转自:http://blog.csdn.net/xys289187120/article/details/6601613
AlertDialog.Builder builder = new AlertDialog.Builder(MainDialog.this);
MultiChoiceID.clear();
builder.setIcon(R.drawable.icon);
builder.setTitle("多项选择");
builder.setMultiChoiceItems(mItems,
new boolean[]{false, false, false, false, false, false, false},
new DialogInterface.OnMultiChoiceClickListener() {
public void onClick(DialogInterface dialog, int whichButton,
boolean isChecked) {
if(isChecked) {
MultiChoiceID.add(whichButton);
showDialog("你选择的id为" + whichButton + " , " + mItems[whichButton]);
}else {
MultiChoiceID.remove(whichButton);
}
}
});
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String str = "";
int size = MultiChoiceID.size();
for (int i = 0 ;i < size; i++) {
str+= mItems[MultiChoiceID.get(i)] + ", ";
}
showDialog("你选择的是" + str);
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
});
builder.create().show();
3.EditText编辑时设置软键盘事件:
转自:http://blog.csdn.net/xys289187120/article/details/6629450
package cn.m15.xys;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.TextView.OnEditorActionListener;
public class KeyBoardActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.keyboard);
EditText editText0 = (EditText)findViewById(R.id.txtTest0);
editText0.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) {
if (arg1 == EditorInfo.IME_ACTION_GO) {
Toast.makeText(KeyBoardActivity.this, "你点了软键盘'去往'按钮",
Toast.LENGTH_SHORT).show();
}
return false;
}
});
EditText editText1 = (EditText)findViewById(R.id.txtTest1);
editText1.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) {
if (arg1 == EditorInfo.IME_ACTION_SEARCH) {
Toast.makeText(KeyBoardActivity.this, "你点了软键盘'搜索'按钮",
Toast.LENGTH_SHORT).show();
}
return false;
}
});
EditText editText2 = (EditText)findViewById(R.id.txtTest2);
editText2.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) {
if (arg1 == EditorInfo.IME_ACTION_SEND) {
Toast.makeText(KeyBoardActivity.this, "你点了软键盘'发送'按钮",
Toast.LENGTH_SHORT).show();
}
return false;
}
});
EditText editText3 = (EditText)findViewById(R.id.txtTest3);
editText3.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) {
if (arg1 == EditorInfo.IME_ACTION_NEXT) {
Toast.makeText(KeyBoardActivity.this, "你点了软键盘'下一个'按钮",
Toast.LENGTH_SHORT).show();
}
return false;
}
});
EditText editText4 = (EditText)findViewById(R.id.txtTest4);
editText4.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) {
if (arg1 == EditorInfo.IME_ACTION_DONE) {
Toast.makeText(KeyBoardActivity.this, "你点了软键盘'完成'按钮",
Toast.LENGTH_SHORT).show();
}
return false;
}
});
EditText editText5 = (EditText)findViewById(R.id.txtTest5);
editText5.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) {
if (arg1 == EditorInfo.IME_ACTION_UNSPECIFIED) {
Toast.makeText(KeyBoardActivity.this, "你点了软键盘'未指定'按钮",
Toast.LENGTH_SHORT).show();
}
return false;
}
});
super.onCreate(savedInstanceState);
}
}