< 定義於.java >
1. 預設TableRow包含6個TextView
2. 在此想讓user按下畫面中的Button後, 能無限增加水平列的widget, 即使會超出螢寬
3. 增加onClick()與onFocusChange()行為
./src/com.tsots.Hori_malloc/Hori_malloc_FC.java (改寫如下)
public class Activity_Hori_malloc extends Activity
{
Context context = Activity_Hori_malloc.this;
HorizontalScrollView scrollView;
TableRow row;
Button bt;
TextView tv1, tv2, tv3, tv4, tv5, tv6;
TextView[] array_tv = {tv1, tv2, tv3, tv4, tv5, tv6};
int categoryPresent = 1;
int rowChildCount = 6;
@Override
public void onCreate(Bundle savedInstanceState)
{
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
scrollView = (HorizontalScrollView) findViewById(R.id.scrollView);
row = (TableRow) scrollView.findViewById(R.id.row);
bt = (Button) findViewById (R.id.bt);
funInitTableItem();
bt.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
funNewTableItem();
}
});
}
//[初始化]設定TextView的格式
public void funInitTableItem()
{
for(int i=0 ; i<rowChildCount ; i++)
{
array_tv[i]= new TextView(context);
array_tv[i].setText("text"+(i+1));
array_tv[i].setHeight(80);
array_tv[i].setWidth(280);
array_tv[i].setGravity(Gravity.CENTER);
array_tv[i].setTextSize(60);
array_tv[i].setFocusable(true);
array_tv[i].setFocusableInTouchMode(true);
array_tv[i].setBackgroundResource(R.drawable.selector_app_category);
row.addView(array_tv[i]);
}
funInitFocusAction();
}
//[初始化]設定TextView的Focus及Click行為
public class Activity_Hori_malloc extends Activity
{
Context context = Activity_Hori_malloc.this;
HorizontalScrollView scrollView;
TableRow row;
Button bt;
TextView tv1, tv2, tv3, tv4, tv5, tv6;
TextView[] array_tv = {tv1, tv2, tv3, tv4, tv5, tv6};
int categoryPresent = 1;
int rowChildCount = 6;
@Override
public void onCreate(Bundle savedInstanceState)
{
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
scrollView = (HorizontalScrollView) findViewById(R.id.scrollView);
row = (TableRow) scrollView.findViewById(R.id.row);
bt = (Button) findViewById (R.id.bt);
funInitTableItem();
bt.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
funNewTableItem();
}
});
}
//[初始化]設定TextView的格式
public void funInitTableItem()
{
for(int i=0 ; i<rowChildCount ; i++)
{
array_tv[i]= new TextView(context);
array_tv[i].setText("text"+(i+1));
array_tv[i].setHeight(80);
array_tv[i].setWidth(280);
array_tv[i].setGravity(Gravity.CENTER);
array_tv[i].setTextSize(60);
array_tv[i].setFocusable(true);
array_tv[i].setFocusableInTouchMode(true);
array_tv[i].setBackgroundResource(R.drawable.selector_app_category);
row.addView(array_tv[i]);
}
funInitFocusAction();
}
//[初始化]設定TextView的Focus及Click行為
public void funInitFocusAction()
{
for(int i=0 ; i<rowChildCount ; i++)
{
final int ii = i;
array_tv[i].setOnFocusChangeListener(new OnFocusChangeListener()
{
@Override
public void onFocusChange(View v, boolean hasFocus)
{
if(hasFocus == true)
{
categoryPresent = ii+1;
Toast.makeText(context, "tvs focus "+categoryPresent, Toast.LENGTH_SHORT).show();
}
}
});
array_tv[i].setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
categoryPresent = ii+1;
Toast.makeText(context, "tvs click "+categoryPresent, Toast.LENGTH_SHORT).show();
}
});
}
}
//[新項目]設定TextView的格式
public void funNewTableItem()
{
//先將TableRow內含的widget清空
row.removeAllViews();
//表示TableRow內含的widget數量+1
rowChildCount++;
for(int i=0 ; i<rowChildCount ; i++)
{
final TextView tv = new TextView(context);
tv.setText("Newtext"+(i+1));
tv.setHeight(80);
tv.setWidth(280);
tv.setGravity(Gravity.CENTER);
tv.setTextSize(60);
tv.setFocusable(true);
tv.setFocusableInTouchMode(true);
tv.setBackgroundResource(R.drawable.selector_app_category);
row.addView(tv);
funNewFocusAction(tv);
}
}
//[新項目]設定TextView的Focus及Click行為
public void funNewFocusAction(final TextView textview)
{
textview.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
Toast.makeText(context, "click "+textview.getText().toString(), Toast.LENGTH_SHORT).show();
}
});
textview.setOnFocusChangeListener(new OnFocusChangeListener()
{
@Override
public void onFocusChange(View v, boolean hasFocus)
{
if(hasFocus == true)
{
Toast.makeText(context, "focus "+textview.getText().toString(), Toast.LENGTH_SHORT).show();
}
}
});
}
}
%%%%%%%%%%%%%%%%%延伸學習%%%%%%%%%%%%%%%%%%%%%%%%%
沒有留言:
張貼留言