【发布时间】:2014-07-29 15:30:53
【问题描述】:
我正在尝试在 Android 中运行一个程序,以在用户按下全部清除按钮时清除 EditText 字段中的所有文本。下面是我到目前为止的代码,目前它没有按计划工作。我是 java 和 android 应用程序开发的新手。如果可能的话,请让我知道如何解决这个问题,并提供示例。非常感谢。
CEMMainActivity.java
public class CEMMainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) { // runs every time the app runs
super.onCreate(savedInstanceState); // loads saved instant state
setContentView(R.layout.activity_cemmain);//sets what the app will look like based on the graphical design created
if (savedInstanceState == null) {// action if app does not start up correctly
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
// Clears all text in EditText fields
Button clearalltext = (Button) findViewById(R.id.cleartext);
clearalltext.setOnClickListener(new OnClickListener() {
public void onClick(View c) {
ViewGroup group = (ViewGroup) findViewById(R.id.cleartext);
clearText(group);
});
}
private void clearText((ViewGroup)group); {
// TODO Auto-generated method stub
for (int i = 0, count = group.getChildCount(); i < count; ++i) {
View view = group.getChildAt(i);
if (view instanceof EditText) {
((EditText)view).setText("");
ViewGroup view;
if(view instanceof ViewGroup && (((ViewGroup)view).getChildCount() > 0))
clearText((ViewGroup)group);
}
}
}
fragment_cemmain.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="clinical.equipment.monitor.CEMMainActivity$PlaceholderFragment" >
.....
<Button
android:id="@+id/cleartext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/findequip"
android:layout_marginTop="56dp"
android:onClick="cleartext"
android:text="@string/cleartext" />
....
</RelativeLayout>
【问题讨论】:
-
显示您的 XML 结构。您的
EditTexts 如何放置在布局中很重要。 -
你尝试过以下解决方案???有什么适合你的吗??
-
感谢您的回复。我为我迟到的回复道歉。我正在尝试您提出的所有解决方案,我会尽快回复您。
标签: java android button android-edittext