我将引导程序与.input-group-addon一起使用,将元素前置或附加到单个表单元素,例如表单的TextArea输入类型。预置简单表示它显示它包含用户禁止编辑的值。
如何在我的安卓应用程序中执行此操作?它是用我的XML还是什么?请帮忙...
以下代码将帮助您做到这一点。
解决方案 1
TextView
将仅显示前缀文本,EditText
可以是用户的选择。
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="@android:color/white" >
<TextView
android:id="@+id/txtTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="http://"
android:textColor="@android:color/darker_gray" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:text="www.google.com"
/>
</LinearLayout>
在上面的布局中,您将看到TextView
具有默认文本,但用户只能用EditText
书写。因此,用户不会对这种技巧一无所知。
解决方案 2
final EditText edt = (EditText) findViewById(R.id.editText1);
edt.setText("http://");
Selection.setSelection(edt.getText(), edt.getText().length());
edt.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
if(!s.toString().contains("http://")){
edt.setText("http://");
Selection.setSelection(edt.getText(), edt.getText().length());
}
}
});
此解决方案不允许用户从其EditText
输入中删除"http://"。
EditText
扩展TextView
扩展View
类。View.java
类都有方法setText
,它几乎适用于扩展View.java
的每个视图。
结语
EditText.setText("your Text here"); //set default value
EditText.setHint("your Text here"); //set hint
在 XML 中
android:text="@string/yourStringText"