EditTextのいくつかの設定を記録します
2665 ワード
背景:EditTextのいくつかの使い方は、よく探しています.そこで、プロジェクトのコードの一部を例に挙げます.レイアウト内にEditTextとButtonが定義されています.次の機能を実現します. EditTextはデフォルトで編集できません. Buttonをクリックし、EditTextを編集可能な状態にし、自動的にフォーカスを取得し、キーボードを起動します. もう一度Buttonをクリックすると、EditTextは編集不可能な状態に戻り、キーボードを非表示にします.
レイアウトファイル(部分):
ButtonのOnClickメソッドの実装:
知識点:
1.コンテンツの複数行表示
2.コンテンツを垂直方向に中央揃え
3.喚起入力方式
4.入力方式を隠す
レイアウトファイル(部分):
<EditText
android:id="@+id/et_photo_desc"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:padding="5dp"
android:textColor="@color/font_color_level_3"
android:background="@drawable/default_input_edit"
android:enabled="false"
android:gravity="center_vertical"
android:inputType="textMultiLine"
android:hint="@string/hint_click_to_add_desc"
android:textSize="@dimen/content_font_size_level_2" />
<Button
android:id="@+id/btn_edit_desc"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_centerInParent="true"
android:textColor="@color/font_color_level_2"
android:text="@string/photo_view_edit_edit_desc"
android:onClick="onEditDescButtonClicked" />
ButtonのOnClickメソッドの実装:
public void onEditDescButtonClicked(View view) {
Button btnEdit = (Button) view;
etPhotoDesc.setEnabled(!etPhotoDesc.isEnabled());
InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (etPhotoDesc.isEnabled()) {
etPhotoDesc.requestFocus();
inputManager.showSoftInput(etPhotoDesc, InputMethodManager.SHOW_FORCED);
} else {
SpotFile spotFile = spotInfo.getFiles().get(currentIndex);
spotFile.setDescription(etPhotoDesc.getText().toString());
inputManager.hideSoftInputFromWindow(etPhotoDesc.getWindowToken(), 0);
}
int titleId = etPhotoDesc.isEnabled() ? R.string.photo_view_edit_save_desc : R.string.photo_view_edit_edit_desc;
btnEdit.setText(titleId);
}
知識点:
1.コンテンツの複数行表示
android:inputType="textMultiLine"
2.コンテンツを垂直方向に中央揃え
android:gravity="center_vertical"
3.喚起入力方式
inputManager.showSoftInput(etPhotoDesc, InputMethodManager.SHOW_FORCED);
4.入力方式を隠す
inputManager.hideSoftInputFromWindow(etPhotoDesc.getWindowToken(), 0);