Van*_*iza 2 android placeholder android-edittext
我想在Android中有一个EditText,我有一个占位符,如果用户没有输入任何内容,它将用作我的输入.在我的案例中,编辑文本用于提示中.
我知道"提示"是一个占位符,但不会将其用作输入.如果我将提示设置为"AAA"并且用户没有输入任何内容并按"OK",我将得到一个空字符串,而不是"AAA".
这是我的代码:
private void promptForUsername(){
// get prompts.xml view
LayoutInflater layoutInflater = LayoutInflater.from(context);
View promptView = layoutInflater.inflate(R.layout.prompts, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
// set prompts.xml to be the layout file of the alertdialog builder
alertDialogBuilder.setView(promptView);
final EditText input = (EditText) promptView.findViewById(R.id.userInput);
//setup a dialog window
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// get user input and set it to result
addEvent(levelSelected, input.getText().toString(), Integer.parseInt(points), time + "s");
sqlLogic();
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
sqlLogic();
dialog.cancel();
}
});
// create an alert dialog
AlertDialog alertD = alertDialogBuilder.create();
alertD.show();
input.setFocusableInTouchMode(true);
input.requestFocusFromTouch();
InputMethodManager lManager = (InputMethodManager)this.getSystemService(Context.INPUT_METHOD_SERVICE);
lManager.showSoftInput(input, 0);
}
Run Code Online (Sandbox Code Playgroud)
version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="10dp" >
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Type username:"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/userInput"
android:layout_width="match_parent"
android:inputType="text"
android:layout_height="wrap_content"
android:maxLength="13"
android:hint="AAA">
<requestFocus />
</EditText>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
在某处添加此静态方法:
static CharSequence getTextOrHint(TextView tv) {
return(TextUtils.isEmpty(getText()) ? getHint() : getText());
}
Run Code Online (Sandbox Code Playgroud)
然后使用该方法检索您的值,如果文本存在则返回该文本,如果不存在,则返回提示.
归档时间: |
|
查看次数: |
25291 次 |
最近记录: |