80,471
社区成员




private Button.OnClickListener addClicked = new Button.OnClickListener() {
@Override
public void onClick(View v) {
ImageView iv_chess = new ImageView(Gobang.this);
iv_chess.setImageResource(R.drawable.white);
iv_chess.setVisibility(0);
Log.v(TAG, "addClicked");
}
};
package com.demo.android.gobang;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class Gobang extends Activity {
private static final String TAG = "Gobang";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViews();
initialize();
setListensers();
}
private Button btn_step;
private ImageView iv_board;
private void findViews() {
btn_step = (Button)findViewById(R.id.botton_step);
iv_board = (ImageView)findViewById(R.id.imageview_board);
}
private void initialize() {
iv_board.setImageResource(R.drawable.board);
btn_step.setText(R.string.step);
}
private void setListensers() {
btn_step.setOnClickListener(addClicked);
}
private Button.OnClickListener addClicked = new Button.OnClickListener() {
@Override
public void onClick(View v) {
Log.v(TAG, "addClicked");
ImageView iv_chess = new ImageView(Gobang.this);
iv_chess.setImageResource(R.drawable.black);
iv_chess.setVisibility(View.VISIBLE);
//取得LinearLayout 物件
LinearLayout ll = (LinearLayout)findViewById(R.id.viewObj);
//将ImageView加入到LinearLayout中
if (null != ll) {
ll.addView(iv_chess);
}
}
};
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/viewObj"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:id="@+id/imageview_board"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
/>
<Button
android:id="@+id/botton_step"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>