LoginSignup
0
0

More than 3 years have passed since last update.

Android 画面タッチでキーボード非表示、画面上の全EditTextと個別EditText対応

Posted at

EditText等でテキスト入力後forcusが外れるとキーボード非表示にする例。

android
public class MainActivity extends AppCompatActivity {
    // キーボード表示を制御するためのオブジェクト
    InputMethodManager inputMethodManager;
    // 背景のレイアウト
    private LinearLayout mainLayout;

   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    //入力エリアEditTextはforcusが外れればキーボードが非表示となる。   
    inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    mainLayout = (LinearLayout) findViewById(R.id.main_layout);
    EditText input1 = (EditText)findViewById(R.id.input1);
  }
}
xml
//上記例のlayoutXMLは下記。LinearLayout android:id="@+id/main_layout"必須
  <?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/main_layout"
    android:orientation="vertical" >

........
        <EditText
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:id="@+id/input1"/>

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0