Android里的类别继承及对象组合
2010-06-01 15:49:00 来源:WEB开发网现在开始写程序了,拿着一根扁担(Layout)和两三个畚箕(一个EditText、和两个Button)。
程序代码如下:
复制到剪贴板 Java代码
package com.misoo.pkaz;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
public class ac01 extends Activity implements OnClickListener {
private final int WC = LinearLayout.LayoutParams.WRAP_CONTENT;
private final int FP = LinearLayout.LayoutParams.FILL_PARENT;
private Button btn, btn2;
private EditText et;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.show_layout();
}
public void show_layout(){
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
et = new EditText(this);
LinearLayout.LayoutParams param =
new LinearLayout.LayoutParams(FP, WC);
layout.addView(et, param);
btn = new Button(this);
LinearLayout.LayoutParams param2 =
new LinearLayout.LayoutParams(WC, WC);
param2.topMargin = 5;
btn.setText("OK");
btn.setBackgroundResource(R.drawable.x_blue3);
btn.setOnClickListener(this);
layout.addView(btn, param2);
btn2 = new Button(this);
btn2.setText("Exit");
btn2.setTextColor(Color.RED);
btn2.setBackgroundResource(R.drawable.x_gray3);
btn2.setOnClickListener(this);
layout.addView(btn2, param2);
setContentView(layout);
}
public void onClick(View v) {
if(v == btn)
setTitle(et.getText());
else if(v == btn2)
finish();
}
}
透过layout扁担的addView()函数就将畚箕一个一个挑起来了。
更多精彩
赞助商链接