* MainActivity.java
public class MainActivity extends AppCompatActivity {
TextView textView;
String[] items = { "mike", "angel", "crow", "test", "sky" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
Spinner spinner = findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
textView.setText(items[position]);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
textView.setText("");
}
});
}
}
* activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="선택된 아이템"
android:textSize="24sp" />
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
'android' 카테고리의 다른 글
SeekBar 사용법 (0) | 2020.02.03 |
---|---|
입력된 url을 WebView로 열기 (0) | 2020.02.03 |
RecyclerView 리스트(문자목록, 통화목록 ..) (0) | 2020.02.03 |
레이아웃 사용자정의 사용 (0) | 2020.02.03 |
버튼 사용자정의 사용 (0) | 2020.02.03 |