码迷,mamicode.com
首页 > 其他好文 > 详细

kotlin编《第一行代码》(2)

时间:2017-08-02 23:31:43      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:string   parent   ted   代码   dialog   []   match   raw   mat   

一、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"};
  @Override
  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(
    this@MainActivity, R.layout.abc_action_menu_item_layout, data)
  listView.adapter = adapter

}

 

 

 

kotlin编《第一行代码》(2)

标签:string   parent   ted   代码   dialog   []   match   raw   mat   

原文地址:http://www.cnblogs.com/hunter0541/p/7264235.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!