需要向应用

时间:2015-11-10 22:22:25

标签: java android xml

我正在尝试添加一个清除所有EditText框的菜单选项。现在我将它作为Toa​​st项目,弹出一小段时间,但是我需要它来清除输入的任何文本。

MainActivity

package 340.converter;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
    final EditText NumKnots = (EditText) findViewById(R.id.NumKnots);
    final EditText NumMPH = (EditText) findViewById(R.id.NumMPH);
    Button MPHConvert = (Button) findViewById(R.id.MPHConv);

    MPHConvert.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            double Knots = Double.valueOf(NumKnots.getText().toString());
            double MPH = Knots * 1.15078;
            NumMPH.setText(String.valueOf(MPH));

        }
    });

    final EditText NumNMile = (EditText) findViewById(R.id.NumNMile);
    final EditText NumSMiles = (EditText) findViewById(R.id.NumSMiles);
    Button SMilesConv = (Button) findViewById(R.id.SMilesConv);

    SMilesConv.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            double NMile = Double.valueOf(NumNMile.getText().toString());
            double SMiles = NMile * 1.15078;
            NumSMiles.setText(String.valueOf(SMiles));

        }
    });

    final EditText NumCHead = (EditText) findViewById(R.id.NumCHead);
    final EditText NumRHead = (EditText) findViewById(R.id.NumRHead);
    Button RHeadConv = (Button) findViewById(R.id.RHeadConv);

    RHeadConv.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            double CHead = Double.valueOf(NumCHead.getText().toString());
            double RHead = CHead + 180;
            NumRHead.setText(String.valueOf(RHead));

        }
    });
}




@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_Clear)
    {
        Toast.makeText(getApplicationContext(), R.string.action_Clear, Toast.LENGTH_SHORT).show();
        return true;
    }
    else if(id == R.id.action_About)
    {
        Toast.makeText(getApplicationContext(), "Created by:  \n For:  \n Year Created: ", Toast.LENGTH_LONG).show();
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

content_main

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
android:layout_height="match_parent"     android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main" tools:context=".MainActivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="@string/txtKnots"
    android:id="@+id/txtKnots"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/MPHConv"
    android:id="@+id/MPHConv"
    android:layout_below="@+id/txtKnots"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="@string/txtMPH"
    android:id="@+id/txtMPH"
    android:layout_below="@+id/MPHConv"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="@string/txtNMiles"
    android:id="@+id/txtNMiles"
    android:layout_below="@+id/txtMPH"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="32dp" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/SMilesConv"
    android:id="@+id/SMilesConv"
    android:layout_below="@+id/txtNMiles"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="@string/txtSMile"
    android:id="@+id/txtSMile"
    android:layout_below="@+id/SMilesConv"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="@string/txtCHead"
    android:id="@+id/txtCHead"
    android:layout_below="@+id/txtSMile"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="43dp" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/RHeadConv"
    android:id="@+id/RHeadConv"
    android:layout_below="@+id/txtCHead"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="@string/txtRHead"
    android:id="@+id/txtRHead"
    android:layout_below="@+id/RHeadConv"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="numberDecimal"
    android:ems="10"
    android:id="@+id/NumKnots"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/txtRHead"
    android:layout_toEndOf="@+id/txtRHead" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="numberDecimal"
    android:ems="10"
    android:id="@+id/NumMPH"
    android:layout_above="@+id/txtNMiles"
    android:layout_alignRight="@+id/NumKnots"
    android:layout_alignEnd="@+id/NumKnots"
    android:layout_below="@+id/NumKnots"
    android:layout_alignLeft="@+id/NumKnots"
    android:layout_alignStart="@+id/NumKnots" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="numberDecimal"
    android:ems="10"
    android:id="@+id/NumNMile"
    android:layout_above="@+id/SMilesConv"
    android:layout_alignRight="@+id/NumMPH"
    android:layout_alignEnd="@+id/NumMPH"
    android:layout_below="@+id/NumMPH"
    android:layout_alignLeft="@+id/NumMPH"
    android:layout_alignStart="@+id/NumMPH" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="numberDecimal"
    android:ems="10"
    android:id="@+id/NumSMiles"
    android:layout_alignBottom="@+id/txtSMile"
    android:layout_alignRight="@+id/NumNMile"
    android:layout_alignEnd="@+id/NumNMile"
    android:layout_below="@+id/NumNMile"
    android:layout_alignLeft="@+id/NumNMile"
    android:layout_alignStart="@+id/NumNMile" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="numberDecimal"
    android:ems="10"
    android:id="@+id/NumCHead"
    android:layout_above="@+id/RHeadConv"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_below="@+id/NumSMiles"
    android:layout_alignLeft="@+id/NumSMiles"
    android:layout_alignStart="@+id/NumSMiles" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="numberDecimal"
    android:ems="10"
    android:id="@+id/NumRHead"
    android:layout_alignBottom="@+id/txtRHead"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_alignTop="@+id/RHeadConv"
    android:layout_alignLeft="@+id/NumCHead"
    android:layout_alignStart="@+id/NumCHead" />
</RelativeLayout>

的字符串

<resources>
<string name="app_name">Converter</string>
<string name="action_settings">Settings</string>
<string name="action_Clear">Clear Screen</string>
<string name="action_About">About</string>
<string name="txtKnots">Knots</string>
<string name="txtMPH">MPH</string>
<string name="txtNMiles">Nautical Miles</string>
<string name="txtSMile">Statute Miles</string>
<string name="txtRHead">Reciprocal Heading</string>
<string name="txtCHead">Compass Heading</string>
<string name="MPHConv">Convert</string>
<string name="SMilesConv">Convert</string>
<string name="RHeadConv">Convert</string>
</resources>

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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" android:layout_width="match_parent"
android:layout_height="match_parent" android:fitsSystemWindows="true"
tools:context=".MainActivity">

<android.support.design.widget.AppBarLayout android:layout_height="wrap_content"
    android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar android:id="@+id/toolbar"
        android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_main" />

<android.support.design.widget.FloatingActionButton android:id="@+id/fab"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"     android:layout_margin="@dimen/fab_margin"
    android:src="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>




package 340.converter;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
    final EditText NumKnots = (EditText) findViewById(R.id.NumKnots);
    final EditText NumMPH = (EditText) findViewById(R.id.NumMPH);
    Button MPHConvert = (Button) findViewById(R.id.MPHConv);

    MPHConvert.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            double Knots = Double.valueOf(NumKnots.getText().toString());
            double MPH = Knots * 1.15078;
            NumMPH.setText(String.valueOf(MPH));

        }
    });

    final EditText NumNMile = (EditText) findViewById(R.id.NumNMile);
    final EditText NumSMiles = (EditText) findViewById(R.id.NumSMiles);
    Button SMilesConv = (Button) findViewById(R.id.SMilesConv);

    SMilesConv.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            double NMile = Double.valueOf(NumNMile.getText().toString());
            double SMiles = NMile * 1.15078;
            NumSMiles.setText(String.valueOf(SMiles));

        }
    });

    final EditText NumCHead = (EditText) findViewById(R.id.NumCHead);
    final EditText NumRHead = (EditText) findViewById(R.id.NumRHead);
    Button RHeadConv = (Button) findViewById(R.id.RHeadConv);

    RHeadConv.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            double CHead = Double.valueOf(NumCHead.getText().toString());
            double RHead = CHead + 180;
            NumRHead.setText(String.valueOf(RHead));

        }
    });
}




@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    //noinspection SimplifiableIfStatement
    if (id == R.id.action_About)
    {
        Toast.makeText(getApplicationContext(), "Created by: \n For: \n Year Created: ", Toast.LENGTH_LONG).show();
        return true;
    }
    android.view.ViewGroup group = (android.view.ViewGroup)findViewById(R.id.action_Clear);
    for (int k = 0, count = group.getChildCount(); k < count; k++)
    {
        View view = group.getChildAt(k);
        if (view instanceof EditText)
        {
            ((EditText)view).setText("");
        }
    }
    return super.onOptionsItemSelected(item);
}
}

11-09 12:03:57.725 28029-28029 / 340.converter W / EGL_emulation:eglSurfaceAttrib未实现 11-09 12:04:01.415 28029-28029 / 340.converter D / AndroidRuntime:关闭VM 11-09 12:04:01.415 28029-28029 / 340.converter W / dalvikvm:threadid = 1:线程退出未捕获的异常(group = 0xb0caeb20) 11-09 12:04:01.415 28029-28029 / 340.converter E / AndroidRuntime:致命异常:主要 11-09 12:04:01.415 28029-28029 / 340.converter E / AndroidRuntime:流程:340.converter,PID:28029 11-09 12:04:01.415 28029-28029 / 340.converter E / AndroidRuntime:java.lang.NullPointerException 11-09 12:04:01.415 28029-28029 / 340.converter E / AndroidRuntime:at 340.converter.MainActivity.onOptionsItemSelected(MainActivity.java:98) 11-09 12:04:01.415 28029-28029 / 340.converter E / AndroidRuntime:at android.app.Activity.onMenuItemSelected(Activity.java:2600) 11-09 12:04:01.415 28029-28029 / 340.converter E / AndroidRuntime:at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:361) 11-09 12:04:01.415 28029-28029 / 340.converter E / AndroidRuntime:at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:147) 11-09 12:04:01.415 28029-28029 / 340.converter E / AndroidRuntime:at android.support.v7.internal.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:100) 11-09 12:04:01.415 28029-28029 / 340.converter E / AndroidRuntime:at android.support.v7.internal.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:100) 11-09 12:04:01.415 28029-28029 / 340.converter E / AndroidRuntime:at android.support.v7.internal.app.ToolbarActionBar $ 2.onMenuItemClick(ToolbarActionBar.java:73) 11-09 12:04:01.415 28029-28029 / 340.converter E / AndroidRuntime:at android.support.v7.widget.Toolbar $ 1.onMenuItemClick(Toolbar.java:180) 11-09 12:04:01.415 28029-28029 / 340.converter E / AndroidRuntime:at android.support.v7.widget.ActionMenuView $ MenuBuilderCallback.onMenuItemSelected(ActionMenuView.java:761) 11-09 12:04:01.415 28029-28029 / 340.converter E / AndroidRuntime:at android.support.v7.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:811) 11-09 12:04:01.415 28029-28029 / 340.converter E / AndroidRuntime:在android.support.v7.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:153) 11-09 12:04:01.415 28029-28029 / 340.converter E / AndroidRuntime:at android.support.v7.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:958) 11-09 12:04:01.415 28029-28029 / 340.converter E / AndroidRuntime:在android.support.v7.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:948) 11-09 12:04:01.415 28029-28029 / 340.converter E / AndroidRuntime:at android.support.v7.internal.view.menu.MenuPopupHelper.onItemClick(MenuPopupHelper.java:191) 11-09 12:04:01.415 28029-28029 / 340.converter E / AndroidRuntime:在android.widget.AdapterView.performItemClick(AdapterView.java:299) 11-09 12:04:01.415 28029-28029 / 340.converter E / AndroidRuntime:在android.widget.AbsListView.performItemClick(AbsListView.java:1113) 11-09 12:04:01.415 28029-28029 / 340.converter E / AndroidRuntime:在android.widget.AbsListView $ PerformClick.run(AbsListView.java:2911) 11-09 12:04:01.415 28029-28029 / 340.converter E / AndroidRuntime:在android.widget.AbsListView $ 3.run(AbsListView.java:3645) 11-09 12:04:01.415 28029-28029 / 340.converter E / AndroidRuntime:at android.os.Handler.handleCallback(Handler.java:733) 11-09 12:04:01.415 28029-28029 / 340.converter E / AndroidRuntime:at android.os.Handler.dispatchMessage(Handler.java:95) 11-09 12:04:01.415 28029-28029 / 340.converter E / AndroidRuntime:在android.os.Looper.loop(Looper.java:136) 11-09 12:04:01.415 28029-28029 / 340.converter E / AndroidRuntime:at android.app.ActivityThread.main(ActivityThread.java:5001) 11-09 12:04:01.415 28029-28029 / 340.converter E / AndroidRuntime:at java.lang.reflect.Method.invokeNative(Native Method) 11-09 12:04:01.415 28029-28029 / 340.converter E / AndroidRuntime:at java.lang.reflect.Method.invoke(Method.java:515) 11-09 12:04:01.415 28029-28029 / 340.converter E / AndroidRuntime:at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:785) 11-09 12:04:01.415 28029-28029 / 340.converter E / AndroidRuntime:at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 11-09 12:04:01.415 28029-28029 / 340.converter E / AndroidRuntime:at dalvik.system.NativeStart.main(Native Method) 11-09 12:04:01.425 1254-1376 / system_process D /:HostConnection :: get()建立新的主机连接0xb8cb5ba0,tid 1376

1 个答案:

答案 0 :(得分:0)

您可以遍历视图组中的所有子项并清除所有EditText字段:

RelativeLayout
   Linear1  ( for the first 2 buttons )
   Linear 2 ( for the 2 buttons next buttons )
   Linear 3 ( for the last button  )
/RelativeLayout