LinearLayout:水平
垂直
TextViewl文字顯示元件
EditText; 文字輸入元件
getTextl()取得文字:
setText():設定文字
實體元件設定
元件名稱.findViewById(R.id.name);
Button觸發事件
元件‧setOnClickListener(類別名稱);
Button.OnClicListener 類別名稱 = new Button.OnClickListener()
{
public void onClick(View v)
{
[動作函式]
}
};
元件‧方法()=>物件導向第二原理
元件‧屬性=>物件導向第一原理
例:Button,weight=0.5;
package com.example.theoryproject1;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
public class Hope1 extends Activity
{
private EditText et1=null;
@Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.hope1);
et1 = (EditText)findViewById(R.id.editText1);
Button bt1 = (Button)findViewById(R.id.button1);
Button bt2 = (Button)findViewById(R.id.button2);
bt1.setOnClickListener(MyListener1);
}
Button.OnClickListener MyListener1 = new Button.OnClickListener()
{
public void onClick(View v)
{
switch (v.getId())
{
case R.id.button1:
{
break;
}
case R.id.button2:
{
break;
}
}
}
};
}
------------------------------------
顯示元件:Toast
Toast.makeText(this,string,duration).show();
this 鄭在執行中類別元件
string 輸出訊息
duration 顯示時間
Edit Text 取值
CharSequence sname = et1.getText();
序列字元
public void onClick(View v)
{
switch (v.getId())
{
case R.id.button1:
{
CharSequence sname = et1.getText();
Toast.makeText(getApplicationContext(), sname,1000).show();
break;
}
case R.id.button2:
{
et1.setText("");
break;
}
}
}
--------------------------
ImageView 圖片顯示元件
package com.example.theoryproject1;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
public class Imagebasic1 extends Activity
{
private ImageView iv1 =null;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.imagebasic1);
iv1=(ImageView)findViewById(R.id.imageView1);
Button bt1=(Button)findViewById(R.id.button1);
Button bt2=(Button)findViewById(R.id.button2);
Button bt3=(Button)findViewById(R.id.button3);
bt1.setOnClickListener(mybtlistener2);
bt2.setOnClickListener(mybtlistener2);
bt3.setOnClickListener(mybtlistener2);
}
Button.OnClickListener mybtlistener2 = new Button.OnClickListener()
{
@Override
public void onClick(View v) {
switch (v.getId())
{
case R.id.button1:
{
iv1.setImageResource(R.drawable.a10);
break;
}
case R.id.button2:
{
iv1.setImageResource(R.drawable.a11);
break;
}
case R.id.button3:
{
iv1.setImageResource(R.drawable.a12);
break;
}
}
}
};
}
-------------------------------
imagebasic.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg1"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.33"
android:text="影像一"
android:drawableLeft="@drawable/ju27"/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.33"
android:text="影像二"
android:drawableLeft="@drawable/ju28"/>
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.34"
android:text="影像三"
android:drawableLeft="@drawable/ju29"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/a10" />
</LinearLayout>
</LinearLayout>
-----------------------
沒有留言:
張貼留言