我有RelativeLayout
saveCancelBar
,其中包含RelativeLayouts
中也包含的两个按钮。只有点击saveCancelBar
EditText
或焦点时才会显示pageTitle
。出于某种原因,当我单击任何按钮时没有任何反应,并且当onClick
事件(显示Toast
,隐藏软键盘并隐藏EditText
光标)时不会触发我点击它们。
我所指的按钮是"取消"和"保存标题"而saveCancelBar
是包含两者的布局。我确实设置了android:clickable="true"
。这是活动的XML代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:ignore="missingPrefix"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
tools:context="app.travelers.activity.AddPhotoActivity">
<LinearLayout
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:orientation="horizontal">
<TextView
android:id="@+id/back_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/arrow_left"
android:textSize="20sp"
android:clickable="true"
android:textColor="@color/black" />
</LinearLayout>
<LinearLayout
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_below="@id/toolbar"
android:orientation="vertical">
<EditText
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="36dp"
android:background="@color/transparent"
android:maxLength="64"
android:privateImeOptions="nm"
android:inputType="textNoSuggestions|textMultiLine"
android:cursorVisible="false"
app:autoSizeTextType="uniform"
app:autoSizeMinTextSize="16sp"
app:autoSizeMaxTextSize="24sp"
app:autoSizeStepGranularity="4sp"
android:textColor="@color/grey700"/>
<View
android:layout_width="100dp"
android:layout_height="2dp"
android:layout_marginTop="3dp"
android:background="@color/grey700"
/>
<LinearLayout
android:id="@+id/sort"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/header"
android:layout_marginTop="20dp"
android:clickable="true"
android:gravity="bottom"
android:orientation="horizontal"
android:visibility="gone">
<TextView
android:id="@+id/sort_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="3dp"
android:text="List View"
android:textColor="@color/grey700"
android:textSize="14sp"/>
<TextView
android:id="@+id/sort_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/chevron_circle_down"
android:textColor="@color/grey700"
android:textSize="14sp"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/linearLayout">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/add_photo_layout"
android:layout_marginTop="150dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center"
android:layout_centerInParent="true">
<TextView
android:id="@+id/placeholder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="40dp"
android:layout_marginEnd="40dp"
android:layout_marginBottom="30dp"
android:background="@null"
android:minLines="0"
android:text="Add your first travel moment to your story"
android:textAlignment="center"
android:textColor="@color/black"
android:textSize="20sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<LinearLayout
android:id="@+id/capture_photo_button"
android:layout_marginBottom="15dp"
android:paddingStart="5dp"
android:paddingEnd="5dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:layout_width="170dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center|center_vertical"
android:background="@drawable/green_pill_thick">
<TextView
android:id="@+id/capture_photo_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="@string/camera"
android:layout_gravity="center_horizontal|center_vertical"
android:textColor="@color/green500"
android:layout_marginEnd="5dp"/>
<TextView
android:id="@+id/capture_photo_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Capture Moment"
android:layout_gravity="center_horizontal|center_vertical"
android:textColor="@color/green500"
android:textAlignment="center"
android:textSize="14sp"/>
</LinearLayout>
<LinearLayout
android:id="@+id/add_photo_button"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingStart="5dp"
android:paddingEnd="5dp"
android:layout_width="170dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center|center_vertical"
android:background="@drawable/blue_pill_thick">
<TextView
android:id="@+id/add_photo_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="@string/photo"
android:layout_gravity="center_horizontal|center_vertical"
android:textColor="@color/blue500"
android:layout_marginEnd="5dp"/>
<TextView
android:id="@+id/add_photo_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add from Gallery"
android:layout_gravity="center_horizontal|center_vertical"
android:textColor="@color/blue500"
android:textAlignment="center"
android:textSize="14sp"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:id="@+id/list_view_layout"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="150dp"
android:paddingBottom="50dp">
<ListView
android:id="@+id/list_view_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stackFromBottom="true"
android:transcriptMode="alwaysScroll">
</ListView>
</RelativeLayout>
</LinearLayout>
<!--Navbar-->
<LinearLayout
android:id="@+id/photo_story_navbar"
android:layout_width="match_parent"
android:layout_height="56dp"
android:background="@drawable/navbar"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:visibility="gone">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="right"
android:paddingStart="15dp"
android:orientation="horizontal"
android:layout_marginEnd="10dp">
<!-- Take Photo Button -->
<LinearLayout
android:id="@+id/nav_capture_button"
android:layout_width="84dp"
android:layout_height="32dp"
android:paddingStart="10dp"
android:orientation="horizontal"
android:background="@drawable/green_pill_button"
android:layout_gravity="center_vertical"
android:gravity="center|center_vertical"
android:paddingEnd="10dp"
android:clickable="true">
<TextView
android:id="@+id/nav_capture_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:text="@string/camera"
android:textColor="@color/green500"
android:layout_gravity="center_horizontal|center_vertical"
android:paddingEnd="5dp"/>
<TextView
android:id="@+id/nav_capture_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Capture"
android:layout_gravity="center_horizontal|center_vertical"
android:textColor="@color/green500"
android:textAlignment="center"
android:textSize="12sp"/>
</LinearLayout>
<!-- Add from Gallery Button -->
<LinearLayout
android:id="@+id/nav_gallery_button"
android:layout_width="84dp"
android:layout_height="32dp"
android:paddingStart="10dp"
android:layout_marginLeft="5dp"
android:orientation="horizontal"
android:background="@drawable/blue_pill"
android:layout_gravity="center_vertical"
android:gravity="center|center_vertical"
android:paddingEnd="10dp"
android:clickable="true">
<TextView
android:id="@+id/nav_gallery_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:text="@string/photo"
android:textColor="@color/blue500"
android:layout_gravity="center_horizontal|center_vertical"
android:paddingEnd="5dp"/>
<TextView
android:id="@+id/nav_gallery_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Gallery"
android:layout_gravity="center_horizontal|center_vertical"
android:textColor="@color/blue500"
android:textAlignment="center"
android:textSize="12sp"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="right"
android:paddingEnd="15dp"
android:orientation="horizontal">
<!-- Review button -->
<LinearLayout
android:id="@+id/nav_review_button"
android:layout_width="84dp"
android:layout_height="32dp"
android:paddingStart="10dp"
android:orientation="horizontal"
android:background="@drawable/red_500_pill"
android:layout_gravity="right"
android:gravity="center|center_vertical"
android:paddingEnd="10dp"
android:clickable="true">
<TextView
android:id="@+id/nav_review_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Review"
android:layout_gravity="center_horizontal|center_vertical"
android:textColor="@color/red500"
android:textAlignment="center"
android:textSize="12sp"/>
<TextView
android:id="@+id/nav_review_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:text="@string/angle_double_right"
android:textColor="@color/red500"
android:layout_gravity="center_horizontal|center_vertical"
android:paddingStart="5dp"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<RelativeLayout
android:id="@+id/save_cancel_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/linearLayout"
android:layout_alignParentStart="true"
android:background="@drawable/black_border_top">
<RelativeLayout
android:id="@+id/save_title_btn"
android:clickable="true"
android:layout_width="84dp"
android:layout_height="32dp"
android:layout_alignParentRight="true"
android:background="@drawable/blue_pill"
android:layout_marginRight="15dp"
android:layout_marginTop="10dp">
<TextView
android:id="@+id/save_title_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_gravity="fill"
android:text="Save Title"
android:textSize="12sp"
android:textColor="#2196F3"/>
</RelativeLayout>
<RelativeLayout
android:id="@+id/cancel_title_btn"
android:layout_width="84dp"
android:layout_height="32dp"
android:clickable="true"
android:layout_alignParentRight="true"
android:background="@drawable/grey_700_pill"
android:layout_marginRight="105dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp">
<TextView
android:id="@+id/cancel_title_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Cancel"
android:layout_gravity="fill"
android:textSize="12sp"
android:textColor="#616161" />
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
具体来说,这些是我的onClick侦听器:
saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(!TextUtils.isEmpty(pageTitle.getText().toString())) {
pageTitle.clearFocus();
getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_HIDDEN);
saveCancelBar.clearAnimation();
saveCancelBar.setVisibility(View.GONE);
Toast.makeText(AddPhotoActivity.this, "Title is successfully saved", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(AddPhotoActivity.this, "Title is too short.", Toast.LENGTH_SHORT).show();
}
}
});
cancelButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pageTitle.clearFocus();
getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_HIDDEN);
saveCancelBar.clearAnimation();
saveCancelBar.setVisibility(View.GONE);
}
});
编辑:我修改了saveButton
&#39; onClickListener
的if条件,并根据@Nilesh和@ Exigente05的建议将.show()
方法添加到祝酒词中。我还添加了pageTitle.clearFocus()
。对代码的更新设法显示祝酒词并隐藏EditText光标,但saveCancelBar
和软键盘仍然存在。
答案 0 :(得分:1)
使用此
if(!TextUtils.isEmpty(pageTitle.getText().toString())) {
getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_HIDDEN);
saveCancelBar.setVisibility(View.GONE);
Toast.makeText(AddPhotoActivity.this, "Title is successfully saved", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(AddPhotoActivity.this, "Title is too Short.", Toast.LENGTH_SHORT).show();
}
而不是
if(pageTitle.getText() != null) {
getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_HIDDEN);
saveCancelBar.setVisibility(View.GONE);
Toast.makeText(AddPhotoActivity.this, "Title is successfully saved", Toast.LENGTH_SHORT);
} else {
Toast.makeText(AddPhotoActivity.this, "Title is too short.", Toast.LENGTH_SHORT);
}
答案 1 :(得分:0)
if(pageTitle.getText() != null)
你检查过这种情况吗?
答案 2 :(得分:0)
我将回答最有趣的发现。
你知道吗?
一切正常,只需要在两个特定的Toast.makeText的末尾添加.show()
。
请仔细查看,在第220和223行,您忘了拨打.show()
这不好笑吗?
另一件事pageTitle.getText() != null
永远都是真的。您需要检查文本长度或emptyText以获得准确的结果。
在saveButton.setOnClickListener
中隐藏键盘和清除焦点更改代码 saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(pageTitle.getText() != null) {
// pageTitle.clearFocus();
View view = ClassName.this.getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
pageTitle.clearFocus();
saveCancelBar.clearAnimation();
saveCancelBar.setVisibility(View.GONE);
Toast.makeText(MainActivity.this, "Title is successfully saved", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Title is too short.", Toast.LENGTH_SHORT).show();
}
}
});
同样在cancelButton.setOnClickListener