onClickListiner 不工作
onClickListiner not working
<FrameLayout
android:id="@+id/comment_container"
android:layout_width="match_parent"
android:layout_height="271dp">
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Comment"
android:id="@+id/comment_add"
android:layout_gravity="right|top"
android:clickable="true"
android:enabled="true" />
<EditText
android:layout_width="253dp"
android:layout_height="48dp"
android:id="@+id/comment_text"
android:layout_gravity="left|top"
android:editable="true"
android:enabled="true" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="218dp"
android:layout_gravity="center_horizontal|bottom">
</FrameLayout>
</FrameLayout>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Comment"
android:id="@+id/comment_add"
android:layout_gravity="right|bottom" />
<EditText
android:layout_width="253dp"
android:layout_height="48dp"
android:id="@+id/comment_text"
android:layout_gravity="left|bottom" />
</FrameLayout>
框架布局包含在相对布局中,无论如何在 activity 代码中:
addComment = (Button) findViewById(R.id.comment_add);
txtComment = (TextView) findViewById(R.id.comment_text);
addComment.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
addTheComment();
}
});
public void addTheComment() {
Meal meal=new Meal();
if(txtComment.getText().toString()!=null){
meal.setComment(txtComment.getText().toString());
Toast.makeText(this ,"adding the comment..." , Toast.LENGTH_SHORT);
}else{
Toast.makeText(this ,"the comment field is empty" , Toast.LENGTH_SHORT);
}
}
我导入了 android 视图,然后你可以看到我在 activity 中做了一个 setonclicklistiner 和 addthecomment() 方法,我试图从编辑文本中获取文本并存储它进入 Meal Class (Parse.com) ,但当我按下模拟器中的按钮时没有得到任何东西,就像 onclick 本身正在工作但没有任何反应
在 addthecomment()
中,您想根据条件显示 Toast 消息。但是用户看不到消息,因为您忘记调用 Toast.show()
。这样做:
if(txtComment.getText().toString()!=null){
....
Toast.makeText(this ,"adding the comment..." ,
Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this ,"the comment field is empty" ,
Toast.LENGTH_SHORT).show();
}
<FrameLayout
android:id="@+id/comment_container"
android:layout_width="match_parent"
android:layout_height="271dp">
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Comment"
android:id="@+id/comment_add"
android:layout_gravity="right|top"
android:clickable="true"
android:enabled="true" />
<EditText
android:layout_width="253dp"
android:layout_height="48dp"
android:id="@+id/comment_text"
android:layout_gravity="left|top"
android:editable="true"
android:enabled="true" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="218dp"
android:layout_gravity="center_horizontal|bottom">
</FrameLayout>
</FrameLayout>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Comment"
android:id="@+id/comment_add"
android:layout_gravity="right|bottom" />
<EditText
android:layout_width="253dp"
android:layout_height="48dp"
android:id="@+id/comment_text"
android:layout_gravity="left|bottom" />
</FrameLayout>
框架布局包含在相对布局中,无论如何在 activity 代码中:
addComment = (Button) findViewById(R.id.comment_add);
txtComment = (TextView) findViewById(R.id.comment_text);
addComment.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
addTheComment();
}
});
public void addTheComment() {
Meal meal=new Meal();
if(txtComment.getText().toString()!=null){
meal.setComment(txtComment.getText().toString());
Toast.makeText(this ,"adding the comment..." , Toast.LENGTH_SHORT);
}else{
Toast.makeText(this ,"the comment field is empty" , Toast.LENGTH_SHORT);
}
}
我导入了 android 视图,然后你可以看到我在 activity 中做了一个 setonclicklistiner 和 addthecomment() 方法,我试图从编辑文本中获取文本并存储它进入 Meal Class (Parse.com) ,但当我按下模拟器中的按钮时没有得到任何东西,就像 onclick 本身正在工作但没有任何反应
在 addthecomment()
中,您想根据条件显示 Toast 消息。但是用户看不到消息,因为您忘记调用 Toast.show()
。这样做:
if(txtComment.getText().toString()!=null){
....
Toast.makeText(this ,"adding the comment..." ,
Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this ,"the comment field is empty" ,
Toast.LENGTH_SHORT).show();
}