处理具有视图可见性的 Onclick 事件 gone/visible
Handle Onclick event with a view visibility gone/visible
我解释一下我的问题。
我有一个视图,其中有两个按钮和两个 editText。
如果我触摸 button1,我想显示 edidText1。
如果我触摸 button2,我想显示 edidText2。
在我的布局中,edidText1 的可见性可见,edidText2 的可见性消失了。
My_Layout.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_gravity="center"
android:layout_height="match_parent">
<Button
android:id="@+id/btn_comments1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/form_comments"/>
<Button
android:id="@+id/btn_comments2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<EditText
android:id="@+id/et_comments1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="visible"
android:gravity="top" />
<EditText
android:id="@+id/et_comments2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:gravity="top" />
</LinearLayout>
在我的 onCreate 函数中
final EditText etComments1 = (EditText) alertDialogView.findViewById(R.id.et_comments);
etComments1.setText(comment1);
etComments1.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Toast.makeText(context, "TODO_1", Toast.LENGTH_LONG).show();
}
});
final EditText etComments2 = (EditText) alertDialogView.findViewById(R.id.et_mes_corr);
etComments2.setText(comment2);
etComments2.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Toast.makeText(context, "TODO_2", Toast.LENGTH_LONG).show();
}
});
final Button btnComments1 = (Button) alertDialogView.findViewById(R.id.btn_comments1);
final Button btnComments2 = (Button) alertDialogView.findViewById(R.id.btn_comments2);
btnComments2.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
etComments1.setVisibility(View.INVISIBLE);
etComments2.setVisibility(View.VISIBLE);
}
});
btnComments1.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
etComments1.setVisibility(View.VISIBLE);
etComments2.setVisibility(View.INVISIBLE);
}
});
当第一次点击我的 etComments1 时,TODO_1 被调用。
在我单击 btnComments2 以显示 etComments2 之后,当我单击它时,不会调用 TODO_2。
在我点击我的 btnComments1 显示 etComments1 之后,当我点击它时,TODO_1 没有被调用。
所以我在想 view.Gone 可以来。所以我在按钮的onClick中也实现了thi事件,但问题还是一样。
有人可以帮助我吗?
编辑
问题出在布局文件和代码中。您已将 visibility:gone 用于 edittext2,如果您阅读了 doc,那么您将理解 "gone" 表示 "Completely hidden, as if the view had not been added."
因此,如果您想保留视图,则不应使用 "gone" 属性。另外为什么你声明你的变量是最终的?这不是必需的。希望对您有所帮助:)
布局:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText
android:id="@+id/et_comments1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="visible"
android:gravity="top" />
<EditText
android:id="@+id/et_comments2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="invisible"
android:gravity="top" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_gravity="center"
android:layout_height="match_parent">
<Button
android:id="@+id/btn_comments1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button 1"/>
<Button
android:text="button 2"
android:id="@+id/btn_comments2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
主要活动:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
/**
* Created by sonnet on 2/17/15.
*/
public class MainActivity extends Activity {
Button button1;
Button button2;
EditText editText1;
EditText editText2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.btn_comments1);
button2 = (Button) findViewById(R.id.btn_comments2);
editText1 = (EditText) findViewById(R.id.et_comments1);
editText2 = (EditText) findViewById(R.id.et_comments2);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editText1.setVisibility(View.VISIBLE);
editText2.setVisibility(View.INVISIBLE);
int height = 0;
int width = 0;
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, height);
editText2.setLayoutParams(params);
height = FrameLayout.LayoutParams.WRAP_CONTENT;
width = FrameLayout.LayoutParams.MATCH_PARENT;
params = new LinearLayout.LayoutParams(width, height);
editText1.setLayoutParams(params);
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editText1.setVisibility(View.INVISIBLE);
editText2.setVisibility(View.VISIBLE);
int height = 0;
int width = 0;
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, height);
editText1.setLayoutParams(params);
height = FrameLayout.LayoutParams.WRAP_CONTENT;
width = FrameLayout.LayoutParams.MATCH_PARENT;
params = new LinearLayout.LayoutParams(width, height);
editText2.setLayoutParams(params);
}
});
editText1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "edittext1 is clicked", Toast.LENGTH_SHORT).show();
}
});
editText2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "edittext2 is clicked", Toast.LENGTH_SHORT).show();
}
});
}
}
添加 android:focusable="false"
和 android:clickable="true"
后,我认为问题在于 EditText
在第一次点击时获得焦点,然后第二次点击将调用侦听器。
尝试添加
android:focusableInTouchMode="false"
到您的 EditText
并且应该允许单击以呼叫听众。
我解释一下我的问题。
我有一个视图,其中有两个按钮和两个 editText。 如果我触摸 button1,我想显示 edidText1。 如果我触摸 button2,我想显示 edidText2。
在我的布局中,edidText1 的可见性可见,edidText2 的可见性消失了。
My_Layout.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_gravity="center"
android:layout_height="match_parent">
<Button
android:id="@+id/btn_comments1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/form_comments"/>
<Button
android:id="@+id/btn_comments2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<EditText
android:id="@+id/et_comments1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="visible"
android:gravity="top" />
<EditText
android:id="@+id/et_comments2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:gravity="top" />
</LinearLayout>
在我的 onCreate 函数中
final EditText etComments1 = (EditText) alertDialogView.findViewById(R.id.et_comments);
etComments1.setText(comment1);
etComments1.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Toast.makeText(context, "TODO_1", Toast.LENGTH_LONG).show();
}
});
final EditText etComments2 = (EditText) alertDialogView.findViewById(R.id.et_mes_corr);
etComments2.setText(comment2);
etComments2.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Toast.makeText(context, "TODO_2", Toast.LENGTH_LONG).show();
}
});
final Button btnComments1 = (Button) alertDialogView.findViewById(R.id.btn_comments1);
final Button btnComments2 = (Button) alertDialogView.findViewById(R.id.btn_comments2);
btnComments2.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
etComments1.setVisibility(View.INVISIBLE);
etComments2.setVisibility(View.VISIBLE);
}
});
btnComments1.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
etComments1.setVisibility(View.VISIBLE);
etComments2.setVisibility(View.INVISIBLE);
}
});
当第一次点击我的 etComments1 时,TODO_1 被调用。 在我单击 btnComments2 以显示 etComments2 之后,当我单击它时,不会调用 TODO_2。 在我点击我的 btnComments1 显示 etComments1 之后,当我点击它时,TODO_1 没有被调用。
所以我在想 view.Gone 可以来。所以我在按钮的onClick中也实现了thi事件,但问题还是一样。
有人可以帮助我吗?
编辑
问题出在布局文件和代码中。您已将 visibility:gone 用于 edittext2,如果您阅读了 doc,那么您将理解 "gone" 表示 "Completely hidden, as if the view had not been added."
因此,如果您想保留视图,则不应使用 "gone" 属性。另外为什么你声明你的变量是最终的?这不是必需的。希望对您有所帮助:)
布局:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText
android:id="@+id/et_comments1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="visible"
android:gravity="top" />
<EditText
android:id="@+id/et_comments2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="invisible"
android:gravity="top" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_gravity="center"
android:layout_height="match_parent">
<Button
android:id="@+id/btn_comments1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button 1"/>
<Button
android:text="button 2"
android:id="@+id/btn_comments2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
主要活动:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
/**
* Created by sonnet on 2/17/15.
*/
public class MainActivity extends Activity {
Button button1;
Button button2;
EditText editText1;
EditText editText2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.btn_comments1);
button2 = (Button) findViewById(R.id.btn_comments2);
editText1 = (EditText) findViewById(R.id.et_comments1);
editText2 = (EditText) findViewById(R.id.et_comments2);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editText1.setVisibility(View.VISIBLE);
editText2.setVisibility(View.INVISIBLE);
int height = 0;
int width = 0;
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, height);
editText2.setLayoutParams(params);
height = FrameLayout.LayoutParams.WRAP_CONTENT;
width = FrameLayout.LayoutParams.MATCH_PARENT;
params = new LinearLayout.LayoutParams(width, height);
editText1.setLayoutParams(params);
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editText1.setVisibility(View.INVISIBLE);
editText2.setVisibility(View.VISIBLE);
int height = 0;
int width = 0;
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, height);
editText1.setLayoutParams(params);
height = FrameLayout.LayoutParams.WRAP_CONTENT;
width = FrameLayout.LayoutParams.MATCH_PARENT;
params = new LinearLayout.LayoutParams(width, height);
editText2.setLayoutParams(params);
}
});
editText1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "edittext1 is clicked", Toast.LENGTH_SHORT).show();
}
});
editText2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "edittext2 is clicked", Toast.LENGTH_SHORT).show();
}
});
}
}
添加 android:focusable="false"
和 android:clickable="true"
后,我认为问题在于 EditText
在第一次点击时获得焦点,然后第二次点击将调用侦听器。
尝试添加
android:focusableInTouchMode="false"
到您的 EditText
并且应该允许单击以呼叫听众。