使用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 文件。menu
res
@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);
}
}