gpt4 book ai didi

java - onClickListener 不工作

转载 作者:行者123 更新时间:2023-12-02 05:01:29 25 4
gpt4 key购买 nike

   <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 View ,然后如你所见,我做了一个setonclicklistiner,然后在 Activity 中添加了comment()方法,我试图从edittext中获取文本并将其存储到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();
}

关于java - onClickListener 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28251556/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com