Android_View初步(一)第一季重制版

来源:互联网 发布:网站编程好学吗 编辑:程序博客网 时间:2024/04/30 21:00

1.View的基本概念



View的种类:

View是所有控件的父件


2.在Activity当中获取代表View的对象


3.设置View的属性

有两种方法设置View的属性:
1.在xml中设置
2.在代码中设置
eg):
xml中设置 
android:text="hello_world";

代码中设置
textview = (TextView) findViewById(R.id.textview);
textview.setText("Hello Mars");
运行之后显示 Hello Mars

4.为View设置监听器

什么是监听器?

所谓的监听器是一种对象,它监控着控件对象状态的变化,当控件发生了某种变化,比如按钮被点击了,控件就会通知监听器,监听器就会执行某些操作


导入快捷键:Ctrl +Shift + O

package com.marschen.s01_e05_view;import android.app.Activity;import android.os.Bundle;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;public class MainActivity extends Activity {private TextView textView;private Button button;int count = 0;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);textView = (TextView)findViewById(R.id.textView);button = (Button)findViewById(R.id.button);ButtonListener buttonListener = new ButtonListener();button.setOnClickListener(buttonListener);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.activity_main, menu);return true;}class ButtonListener implements OnClickListener{@Overridepublic void onClick(View v) {count++;textView.setText(count + "");}}}

<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:orientation="vertical"    tools:context=".MainActivity" >    <TextView        android:id="@+id/textView"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:background="#FF0000"        android:text="0" />        <Button         android:id="@+id/button"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="button"/></LinearLayout>



0 0