Android基礎----View

View:文本等都是View的子類

Activity_main.xml

<LinearLayoutxmlns: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="@string/hello_world"/>

 

</LinearLayout>

 

mainAcitivity.java

package com.Jason.s01_e05_acitivity;

 

import android.os.Bundle;

import android.app.Activity;

import android.view.Menu;

import android.widget.TextView;

 

public class MainActivity extends Activity {

 

private TextView textView;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

 

textView=(TextView)findViewById(R.id.textView);

textView.setText("Hello Jason");

 }

 

 

    @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;

    }

 

}

2.監聽器:

 

Eclipse導入一個包之類的使用ctrl+shift+o快捷鍵

package com.Jason.s01_e05_acitivity;

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;

 

publicclass MainActivity extends Activity {

 

private TextView textView;

private Button button;

intcount=0;

@Override

protectedvoid 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);

 }

 

 

@Override

publicboolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

        getMenuInflater().inflate(R.menu.main, menu);

returntrue;

    }

class ButtonListener implements OnClickListener{

 

@Override

publicvoid onClick(View v) {

// TODO Auto-generated method stub

count++;

textView.setText(count+"");

 

}

 

 

    }

 

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章