我是android和java的新手但是我不明白如何实现这个方法,而不是当我点击应用程序中的按钮时它是一个错误。 这是代码
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity {
public TextView theQuestion;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
theQuestion = (TextView) findViewById(R.id.Title);
}//end of onCreate
public void action(View theQuestion){
theQuestion.setVisibility(View.VISIBLE);
}
public void antiAction(View theQuestion){
theQuestion.setVisibility(View.INVISIBLE);
}
}//the end
<Button
style="?android:attr/buttonStyleSmall"
android:onClick="action"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go"
android:id="@+id/button1" />
<Button
style="?android:attr/buttonStyleSmall"
android:onClick="antiAction"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="undo"
android:id="@+id/button2" />
和xml代码
请帮帮我
答案 0 :(得分:0)
你必须按下按钮,
Button button = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
或转到res - layout - main.xml并在按钮中键入此内容 机器人:的onClick =&#34;按钮&#34;
并取名为
public void button (view v )
{
type your code
}
答案 1 :(得分:0)
只需在布局XML中的Button上添加onClick="action"
。
要点击按钮,有两种类型 1.在布局XML中添加onClick =“action” 然后在Java文件中定义动作mathod
public void action(View view){
//where view define this button define in xml
// Perform action on click
}
初始化按钮后,添加onCLickListener
按钮按钮=(按钮)findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
});
答案 2 :(得分:0)
以上都没有奏效,但确实如此 import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}//end of onCreate
public void action(View view){
TextView theQuestion = (TextView) findViewById(R.id.Title);
theQuestion.setVisibility(View.VISIBLE);
}
public void antiAction(View view){
TextView theQuestion = (TextView) findViewById(R.id.Title);
theQuestion.setVisibility(View.INVISIBLE);
}
}//the end