Android-第二天

1.Activity是Android组件中最基本也是最为常见用的四大组件(Activity,Service服务,Content Provider内容提供者,BroadcastReceiver广播接收器)之一。

Activity中所有操作都与用户密切相关,是一个负责与用户交互的组件,可以通过setContentView(View)来显示指定控件
在一个android应用中,一个Activity通常就是一个单独的屏幕,它上面可以显示一些控件也可以监听并处理用户的事件做出响应。Activity之间通过Intent进行通信。
 
2.程序1
activity_main.xml
 1 <?xml version="1.0" encoding="utf-8"?>  
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
 3     android:orientation="vertical"  
 4     android:layout_width="fill_parent"  
 5     android:layout_height="fill_parent"  
 6     >  
 7 <EditText android:text="EditText01" android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/edtInput"></EditText>  
 8 <LinearLayout android:id="@+id/LinearLayout01" android:layout_height="wrap_content" android:layout_width="fill_parent" android:gravity="center">  
 9 <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Show" android:id="@+id/btnShow"></Button>  
10 <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Clear" android:id="@+id/btnClear"></Button>  
11 </LinearLayout>
12 <AbsoluteLayout android:id="@+id/AbsoluteLayout01" android:layout_height="wrap_content" android:layout_width="fill_parent" >   
13 <TextView android:text="TextView01" 
14     android:id="@+id/TextView01" 
15     android:layout_height="wrap_content" 
16     android:layout_y="10px" 
17     android:layout_width="wrap_content" 
18     android:layout_x="110px">   
19 </TextView>   
20 </AbsoluteLayout>    
21 </LinearLayout>


MainActivity.java

public class MainActivity extends ActionBarActivity {

    Button btnShow;  
    Button btnClear;  
    EditText edtInput;  
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        btnShow = (Button)findViewById(R.id.btnShow);
        btnClear = (Button)findViewById(R.id.btnClear);
        edtInput = (EditText)findViewById(R.id.edtInput);
        btnShow.setOnClickListener(new ClickListener());
        //绑定匿名的监听器,并执行您所要在点击按钮后执行的逻辑代码
btnClear.setOnClickListener(
new ClickListener()); // if (savedInstanceState == null) { // getSupportFragmentManager().beginTransaction() // .add(R.id.container, new PlaceholderFragment()).commit(); // } } class ClickListener implements OnClickListener // 使用自定义内部类继承监听器抽象类,并实现抽象方法。
{
public void onClick(View v) { if(v==btnShow) { new AlertDialog.Builder(MainActivity.this) .setIcon(android.R.drawable.ic_dialog_alert) .setTitle("Information") .setMessage(edtInput.getText()) .show(); } else if(v==btnClear) { edtInput.setText("HelloAndroid"); } } }

 http://blog.csdn.net/hellogv/article/details/4515763

监听器是个抽象类,它包含了一个事件触发时系统会去调用的函数      

每个监听器都是跳转到一个新的activity

监听器的函数命名规则是setOn****Listener

 或者是   也可以使用Java提供的抽象类的匿名实现

 1  @Override
 2  2  public void onCreate(Bundle savedInstanceState) {
 3  3    super.onCreate(savedInstanceState);
 4  4    setContentView(R.layout.main);
 5  5    Button btn = (Button)findViewById(R.id.btnOK);
 6  6    //绑定匿名的监听器,并执行您所要在点击按钮后执行的逻辑代码
 7  7     btn.setOnClickListener(new View.OnClickListener() {
 8  8             
 9  9     @Override
10 10      public void onClick(View arg0) {
11 11      // TODO Auto-generated method stub
12 12      Toast.makeText(MyActivity.this, "点击了按钮", Toast.LENGTH_LONG).show();
13 13     }
14 14   });
15 15  }

 Toast 是一个 浮动显示的View 视图(少量信息)。

程序2

main.xml

 1 <?xml version="1.0" encoding="utf-8"?>  
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
 3     android:orientation="vertical"  
 4     android:layout_width="fill_parent"  
 5     android:layout_height="fill_parent"  
 6     >  
 7      <TableLayout   
 8              android:id="@+id/TableLayout01"   
 9              android:layout_width="fill_parent"   
10              android:layout_height="wrap_content">  
11      </TableLayout>  
12 </LinearLayout> 
 1 package com.LayoutDemo;  
 2 import com.LayoutDemo.R;  
 3 import android.app.Activity;  
 4 import android.os.Bundle;  
 5 import android.view.ViewGroup;  
 6 import android.widget.TableLayout;  
 7 import android.widget.TableRow;  
 8 import android.widget.TextView;  
 9 public class LayoutDemo extends Activity {  
10     /** Called when the activity is first created. */  
11     private final int WC = ViewGroup.LayoutParams.WRAP_CONTENT;  
12     private final int FP = ViewGroup.LayoutParams.FILL_PARENT;  
13       
14     @Override  
15     public void onCreate(Bundle savedInstanceState) {  
16         super.onCreate(savedInstanceState);  
17         setContentView(R.layout.main);  
18         //新建TableLayout01的实例   
19         TableLayout tableLayout = (TableLayout)findViewById(R.id.TableLayout01);  
20         //全部列自动填充空白处   
21         tableLayout.setStretchAllColumns(true);  
22         //生成10行,8列的表格   
23         for(int row=0;row<10;row++)  
24         {  
25             TableRow tableRow=new TableRow(this);  
26             for(int col=0;col<8;col++)  
27             {  
28                 //tv用于显示   
29                 TextView tv=new TextView(this);  
30                 tv.setText("("+col+","+row+")");  
31                 tableRow.addView(tv);  
32             }  
33             //新建的TableRow添加到TableLayout   
34             tableLayout.addView(tableRow, new TableLayout.LayoutParams(FP, WC));  
35         }  
36     }  
37 }  

http://blog.csdn.net/hellogv/article/details/4523745

3.

android:id="@+id/btnOK"

+表示通过它来生成静态资源,如果没有+,表示使用的是指定位置的静态资源,一般为控件赋ID时,都使用+这个方法

原文地址:https://www.cnblogs.com/ejllen/p/3726989.html