我正在为纸牌游戏“黑桃”开发一个记分应用程序。
在每只手的末尾单击按钮时,我想将有关手的一些信息存储到TextView中,并在ScrollView中显示所有手部历史记录。
如何修改.XML布局或以其他方式将新TextView添加到代码中的布局?
我试过......
public static LinearLayout gamehistory;
gamehistory = (LinearLayout) findViewById(R.id.gamehistory);
public void setClickListener1(){
lockwagersButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.results_layout);
gameHistory.addView(new TextView(...)); // I can't get this to work
...
谢谢! -K.H。
答案 0 :(得分:2)
以下代码添加textview onclick:
public class ViewOnClick extends Activity {
LinearLayout.LayoutParams layoutParams;
LinearLayout ll;
static int i;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b = (Button)findViewById(R.id.Button01);
ll = (LinearLayout)findViewById(R.id.ll);
layoutParams = new LinearLayout.LayoutParams
(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
b.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
TextView view = new TextView(ViewOnClick.this);
view.setText(++i+" view");
ll.addView(view, layoutParams);
}
});
}
}
答案 1 :(得分:2)
使用以下代码。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:text="Button01"
android:id="@+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
<ScrollView
android:layout_below="@id/Button01"
android:id="@+id/scrollview"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="@+id/gamehistory"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</LinearLayout>
</ScrollView>
</RelativeLayout>
public class Scrollview1 extends Activity {
ScrollView scrollview;
LinearLayout linearLayout;
LinearLayout.LayoutParams layoutParams;
static int i;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
scrollview = (ScrollView)findViewById(R.id.scrollview);
linearLayout = (LinearLayout)findViewById(R.id.gamehistory);
Button b = (Button)findViewById(R.id.Button01);
layoutParams = new LinearLayout.LayoutParams
(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
b.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
TextView view = new TextView(Scrollview1.this);
view.setText(++i+" view");
linearLayout.addView(view, layoutParams);
}
});
}
}
答案 2 :(得分:0)
public void setClickListeners()
{
lockwagersButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Do Stuff
lockresultsButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
calculateScores();
setContentView(R.layout.scoreboard_layout);
SettingsActivity.currentdealer.getNextDealer();
setSpinners1();
findDisplays();
setPlayerNamesScoreboard();
lockwagersButton = (Button) findViewById(R.id.lockwagers);
displayScores();
checkForWinner();
saveHandHistory();
setClickListeners(); // This is the recursion <------
}
}
});
}
});
}
基于你给我的东西
public void saveHandHistory(){
scrollview = (ScrollView) findViewById(R.id.ScrollView1);
gamehistory = (LinearLayout) findViewById(R.id.gamehistory);
layoutparams = new LinearLayout.LayoutParams (LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
TextView handHistory = new TextView(ScoreboardActivity.this);
handHistory.setText("Hand History Will Go Here");
gamehistory.addView(handHistory, layoutparams);
}