0
感謝您的幫助,但我仍然掙扎。我這樣做:增加TextView與按鈕點擊
按鈕在XML:
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="43dp"
android:background="@android:color/white"
android:onClick="incrementScore"
android:text="@string/player" />
代碼在main(右末):
WhenI按下按鈕的應用程序崩潰。我應該在哪裏具體編碼?
Context context;
TextView tv;
Button incrementer;
public void incrementScore(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_home);
tv=(TextView)findViewById(R.id.textView1);
incrementer=(Button)findViewById(R.id.button1);
context=this;
incrementer.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
String present_value_string = tv.getText().toString();
int present_value_int = Integer.parseInt(present_value_string);
present_value_int++;
tv.setText(String.valueOf(present_value_int));
}
});
}