1. 程式人生 > >kotlin編《第一行代碼》(2)

kotlin編《第一行代碼》(2)

listview () font otl layout array ont [] ace

一、alertdialog

java:

  AlertDialog.Builder dialog=new AlertDialog.Builder(MainActivity.this);

  dialog.setTitle("This is Dialog");

  dialog.setMessage("Something important.");

  dialog.setCancelable(false);

  dialog.setPositiveButton("OK",new DialogInterface.OnClickListener(){

    public void onClick(DialogInterface dialog,int which){

    }

  });

  dialog.setNegativeButton("Cancel",new DialogInterface.OnclickListener(){

    public void onClick(DialogInterface diailog,int which){

    }

  });

  dialog.show();

kotlin:

fun alert(v:View?){
  val dialog1=AlertDialog.Builder(this)
  dialog1.setTitle("This is Dialog")
  dialog1.setMessage("Something important.")
  dialog1.setCancelable(false)
  dialog1.create().show()
}

二、listview

java:

  <ListView
    android:id="@+id/list_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

  />

  private String[] data={"Apple","Banana","Orange","Watermelon","Pear","Grape","Pineapple",
    "Strawberry","Cherry","Mango","Apple","Banana","Orange","Watermelon",


    "Pear","Grape","Pineapple","Strawberry","Cherry","Mango"};
[email protected]
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ArrayAdapter<String> adapter=new ArrayAdapter<String>(
    MainActivity.this,R.layout.abc_action_menu_item_layout,data);
    ListView listView=(ListView)findViewById(R.id.list_view);
    listView.setAdapter(adapter);
  }

kotlin:

  private val data = arrayOf("Apple", "Banana", "Orange", "Watermelon", "Pear", "Grape", "Pineapple",

     "Strawberry", "Cherry", "Mango", "Apple", "Banana", "Orange", "Watermelon",

    "Pear", "Grape", "Pineapple", "Strawberry", "Cherry", "Mango")
  override fun onCreate(savedInstanceState: Bundle?) {
  val adapter = ArrayAdapter(
[email protected], R.layout.abc_action_menu_item_layout, data)
  listView.adapter = adapter

}

 

kotlin編《第一行代碼》(2)