package com.example.app123;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
EditText input;
EditText output;
Button one;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
input = (EditText) findViewById(R.id.editText1);
output = (EditText) findViewById(R.id.editText2);
one = (Button) findViewById(R.id.button1);
one.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (v == one) {
Context context1 = getApplicationContext();
CharSequence text1 = "Please enter a valid number";
int duration = Toast.LENGTH_SHORT;
final Toast toast = Toast.makeText(context1, text1,
duration);
toast.show();
int inputValue = Integer.parseInt(input.getText()
.toString());
int value = inputValue * inputValue;
output.setText(value);
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it ispresent.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
答案 0 :(得分:0)
如果(v == one)仅使用此按钮,则在按钮单击代码中删除此条件
input=(EditText)findViewById(R.id.editText1);
output = (EditText) findViewById(R.id.editText2);
one = (Button) findViewById(R.id.button1);
one.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Context context1 = getApplicationContext();
CharSequence text1 = "Please enter a valid number";
int duration = Toast.LENGTH_SHORT;
final Toast toast = Toast.makeText(context1, text1,
duration);
toast.show();
int inputValue = Integer.parseInt(input.getText()
.toString());
int value = inputValue * inputValue;
output.setText(value);
}
});
我希望它为你工作。
答案 1 :(得分:0)
如果您有多个按钮:
Button b1=(Button)findviewById(R.id.b1);
Button b2=(Button)findviewById(R.id.b2);
one.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch(v.getid())
case R.id.b1:
CharSequence text1 = "Please enter a valid number";
int duration = Toast.LENGTH_SHORT;
final Toast toast = Toast.makeText(YourActivity.this, text1,
duration);
toast.show();
int inputValue = Integer.parseInt(input.getText()
.toString());
int value = inputValue * inputValue;
output.setText(value);
break;
case R.id.b1:
break;
}
});