从网上复制的简单代码似乎无法正常工作。单击按钮时没有动作,但正确更改了文本。怎么了?
package android.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class main extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button closeButton = (Button)this.findViewById(R.id.button1);
closeButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Klik!", Toast.LENGTH_LONG);
}
});
closeButton.setText("dupa");
}
}
main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button android:layout_width="wrap_content" android:id="@+id/button1" android:layout_height="wrap_content" android:text="@string/button"></Button>
</LinearLayout>
感谢您的帮助, 克里斯
答案 0 :(得分:5)
您需要在Toast上调用.show()。
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Klik!", Toast.LENGTH_LONG).show();
}