0

我是初学者,我正在尝试为 android 制作一个简单的计算器,但他给了我一个语法错误

public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    final EditText e  = (EditText)findViewById(R.id.value);
    final EditText e2 = (EditText)findViewById(R.id.value2);

    getMenuInflater().inflate(R.menu.activity_main, menu);
    Toast.makeText(this,"Welcome",Toast.LENGTH_LONG).show();
    Button welcome = (Button)findViewById(R.id.x);
    welcome.setText("push ");
    welcome.setonclickListener(new View.onclickListener() {

        @Override
        public void onclick(View v) {
            int v1 = Integer.parseInt(e.getText().toString());
            int v2 = Integer.parseInt(e2.getText().toString());
            int v3 = v1 + v2 ;
            Toast.makeText(MainActivity.this,"= " + v3, Toast.LENGTH_LONG).show();

             return true;

         }
         });

}

}

http://imageshack.us/photo/my-images/24/problem00.png/

4

2 回答 2

2

试试:View.OnClickListener()用大写的 O 和 C。同样的事情用setOnClickListener. 这应该够了吧。

于 2012-12-10T16:00:03.433 回答
0

使用OnClickListener(). 您不必指定View. 此外,请确保在覆盖的onClick()方法中使用大写的“C”。确保包含import android.view.View.OnClickListener;在您的导入部分中。Eclipse 应该为您服务,但如果不是,或者您正在使用 Eclipse,请将其手动添加到类的顶部。如果您只是执行以下操作,那应该对您有用:

     welcome.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            int v1 = Integer.parseInt(e.getText().toString());
            int v2 = Integer.parseInt(e2.getText().toString());
            int v3 = v1 + v2;
            Toast.makeText(MainActivity.this, "= " + v3, Toast.LENGTH_LONG).show();

            return true;

        }
    });

--------为完整示例编辑--------

在你的项目中,你有你的主要活动。该活动应使用 XML 资源中的布局。该 XML 资源可能看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />
</LinearLayout>

在您的活动类中,您必须将该资源设置为活动的内容视图,使用setContentView(resID). 假设您的 XML 文件名为 helloworld.xml,您将setContentView(R.layout.helloworld)onCreate(Bundle s)调用super.onCreate(s).

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.helloworld);

}

一旦您设置了活动的视图,您就可以访问该布局的元素(EditTexts、按钮等)。为此,您必须创建 EditText 和 Button 对象(您已经在发布的代码中这样做了,我们只需要在其他地方进行)。继续我的示例,您可以在onCreate(Bundle s)函数中执行以下操作:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.helloworld);

    EditText et1 = (EditText) findViewById(R.id.edittext1);
    EditText et2 = (EditText) findViewById(R.id.edittext2);
    Button but = (Button) findViewById(R.id.button1);

    but.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v)
            et2.setText(et1.getText().toString());
            // this will set the second EditText's text to whatever is in
            // the first EditText, but you could do anything with the value.
        }
    }
}

如果要根据菜单项选择更改按钮的功能,则必须覆盖onOptionsItemSelected(MenuItem item)onCreateOptionsMenu(Menu menu). onCreateOptionsMenu(Menu menu)只需为要打开的菜单按钮创建菜单。onOptionsItemSelected(MenuItem item)实际上决定了当你选择一个菜单项时要做什么。

请参阅此页面上的教程以获取完整的概要http://developer.android.com/guide/topics/ui/menus.html,但这里是他们的示例和一些解释。这些示例不是来自我上面的示例应用程序,而是来自 Android Developer API 页面。我强烈建议您阅读他们的教程。

您所要做的onCreateOptionsMenu(Menu menu)就是告诉 Android 从哪里获取菜单并扩充该资源。这意味着在项目目录中的文件夹中调用game_menu了一个 XML 文件。menures

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.game_menu, menu);
    return true;
}

onOptionsItemSelected(MenuItem item)是菜单逻辑的核心所在,让您可以根据选择的菜单项执行不同的任务。在此示例中,game_menu上述函数中提到的 XML 文件具有名为new_game和的菜单项help

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
        case R.id.new_game:
            newGame();
            return true;
        case R.id.help:
            showHelp();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}
于 2012-12-10T16:06:52.290 回答