2015-05-02 62 views
2

我试图使多个按钮点击时生成一个textview。我用Mattia的帮助:)我做对了吗?也可以是计数器是总介绍,目前在代码中的计数器不断增加一个(因为它的计数器++),但如果我改变它来对付+ 10,它不会工作:(能有人帮助这一点。与总共生成的多个按钮

代码

public class MainActivity extends ActionBarActivity { 

int counter = 0; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    final LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linear_layout); 

    Button button = (Button) findViewById(R.id.add_button); 
    Button button1 = (Button) findViewById(R.id.test2); 
    button1.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      counter++; 

      TextView textView = new TextView(MainActivity.this); 
      textView.setText("You have added noodles and your total is" + counter); 
      textView.setBackgroundColor(Color.parseColor("#FF0000")); 
      textView.setTextColor(Color.parseColor("#00FF00")); 

      linearLayout.addView(textView); 
     } 
    }); 
    button.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      counter++; 

      TextView textView = new TextView(MainActivity.this); 
      textView.setText("you have added hotdog and your total is " + counter); 
      textView.setBackgroundColor(Color.parseColor("#FF0000")); 
      textView.setTextColor(Color.parseColor("#00FF00")); 

      linearLayout.addView(textView); 
     } 
    }); 
} 

和主文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/linear_layout" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical"> 

<Button 
    android:id="@+id/add_button" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="Add TextView" /> 

<Button 
    android:id="@+id/test2" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="Add TextView" /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="New Text" 
    android:id="@+id/textView" /> 

+0

counter + 10?我认为你正在寻找计数器+ = 10 – Gero

+1

一个建议,根据你的代码,当用户点击一个按钮时,你将添加视图布局,你不能够查看所有的视图,因为你没有使用scrollView –

回答

0

你必须键入计数器+ = 10;(注意 '+',它等同于说计数器=计数+ 10)

counter + 10;不保存增量。

1

尝试这样,如果我理解你的问题,它应该工作。

public class MainActivity extends ActionBarActivity { 
    int counter = 0; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 


     Button button = (Button) findViewById(R.id.add_button); 
     Button button1 = (Button) findViewById(R.id.test2); 
     button1.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       counter+= 10; 

       TextView textView = (TextView) findViewById(R.id.textView); 
       textView.setText("You have added noodles and your total is" + counter); 
       textView.setBackgroundColor(Color.parseColor("#FF0000")); 
       textView.setTextColor(Color.parseColor("#00FF00")); 


      } 
     }); 
     button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       counter+=10; 

       TextView textView = (TextView) findViewById(R.id.textView); 
       textView.setText("you have added hotdog and your total is " + counter); 
       textView.setBackgroundColor(Color.parseColor("#FF0000")); 
       textView.setTextColor(Color.parseColor("#00FF00")); 


      } 
     }); 
    } 
}