• SPinner


    public class MainActivity extends Activity {

    private static final String[] bloodType = {"O型","A型","B型","AB型","其他",};
    private TextView textView;
    private Spinner spinner;
    private ArrayAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    textView = (TextView) findViewById(R.id.text);
    spinner = (Spinner) findViewById(R.id.spinner);
    //將可選內容和ArrayAdapter連接
    adapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item,bloodType);
    //將adapter添加到spinner中
    spinner.setAdapter(adapter);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setOnItemSelectedListener(new OnItemSelectedListener(){

    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1,
    int arg2, long arg3) {
    textView.setText("您的血型是"+bloodType[arg2]);
    arg0.setVisibility(View.VISIBLE);
    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
    //沒有選中
    }

    });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
    }

    XML文件:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

    <Spinner
    android:id="@+id/spinner"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"/>
    </LinearLayout>


    }

  • 相关阅读:
    io工具类
    并发高级知识
    HashMap相关源码阅读
    ArrayList和LinkedList部分源码分析性能差异
    我自己的JdbcTemplate
    mysql5.7.20靠谱安装步骤
    NG 转发配置
    SQLite总结
    算是不常用的东西,java中的ResultSet转List
    不常用的技能-【手动编译java类】
  • 原文地址:https://www.cnblogs.com/Smart-Du/p/4302009.html
Copyright © 2020-2023  润新知