我必须编辑文本,如果我点击添加按钮而不在edittext中输入任何内容,我会得到fc。
package com.example.calculator;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Calci extends Activity {
TextView t1;
EditText e1, e2;
Button add, sub, mul, div;
Context c;;
String b, a;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calci);
e1 = (EditText) findViewById(R.id.EditText01);
e2 = (EditText) findViewById(R.id.EditText02);
add = (Button) findViewById(R.id.add);
sub = (Button) findViewById(R.id.sub);
mul = (Button) findViewById(R.id.mul);
div = (Button) findViewById(R.id.div);
t1 = (TextView) findViewById(R.id.textView1);
a = e1.getText().toString();
b = e2.getText().toString();
}
public void doSomething(View v) {
AlertDialog.Builder a1 = new AlertDialog.Builder(c);
if (v.getId() == R.id.add) {
if (a==null & b==null) {
Log.e("hash","asdasd");
// Setting Dialog Title
a1.setTitle("Alert Dialog");
// Setting Dialog Message
a1.setMessage("PLEASE ENTER SOMETHING");
a1.setPositiveButton("yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int button1) {
// if this button is clicked, close
// current activity
dialog.cancel();
}
});
// Showing Alert Message
AlertDialog alertDialog = a1.create();
a1.show();
}
}
else {
int result = Integer.parseInt(a) + Integer.parseInt(b);
t1.setText(Integer.toString(result));
}
}
}
答案 0 :(得分:0)
请删除“public void doSomething(View v)”的内容,
在下面的活动的oncreate()上使用onclicklistener按钮,
add = (Button) findViewById(R.id.add);
add .setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if( e1.getText().toString().equals("") || e2.getText().toString().equals(""))
{
new AlertDialog.Builder(MainActivity.this)
.setTitle("INPUT ALERT")
.setIcon(R.drawable.galleryalart)
.setMessage("PLEASE ENTER THE INPUT??")
.setPositiveButton("OK", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).create().show();
}
else {
int result = Integer.parseInt( e2.getText().toString()) + Integer.parseInt( e2.getText().toString());
t1.setText(result+"");
}
}
});