单击侦听器无法解析为键入

时间:2012-12-10 15:57:52

标签: android

我是初学者,我想尝试为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/

2 个答案:

答案 0 :(得分:2)

尝试:View.OnClickListener()使用大写字母O和C.与setOnClickListener相同。这应该可以解决问题。

答案 1 :(得分: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,Buttons等)。要做到这一点,你必须创建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文件夹的menu文件夹中有一个名为res的XML文件。

@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_gamehelp的菜单项。

@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);
    }
}