我是Android工作室的新手..制作一个calci应用程序。
我有一个不同的算法,无论如何点击数字出现在文本框中,但这没有发生,你能告诉我一个解决方案吗?
java文件:
package bichkondilla.cal;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.text.Editable;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.lang.*;
import android.view.View.OnClickListener;
public class MainActivity extends Activity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onclick(View v) {
Button button = (Button) findViewById(R.id.button);
Button button2 = (Button) findViewById(R.id.button2);
Button button3 = (Button) findViewById(R.id.button3);
Button button4 = (Button) findViewById(R.id.button4);
Button button5 = (Button) findViewById(R.id.button5);
Button button6 = (Button) findViewById(R.id.button6);
Button button7 = (Button) findViewById(R.id.button7);
Button button8 = (Button) findViewById(R.id.button8);
Button button9 = (Button) findViewById(R.id.button9);
Button button10 = (Button) findViewById(R.id.button10);
Button button11 = (Button) findViewById(R.id.button11);
Button button12 = (Button) findViewById(R.id.button12);
Button button13 = (Button) findViewById(R.id.button13);
Button button14 = (Button) findViewById(R.id.button14);
Button button15 = (Button) findViewById(R.id.button15);
Button button16 = (Button) findViewById(R.id.button16);
Button button17 = (Button) findViewById(R.id.button17);
Button button18 = (Button) findViewById(R.id.button18);
Button button19 = (Button) findViewById(R.id.button19);
Button button20 = (Button) findViewById(R.id.button20);
button.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
button4.setOnClickListener(this);
button5.setOnClickListener(this);
button6.setOnClickListener(this);
button7.setOnClickListener(this);
button8.setOnClickListener(this);
button9.setOnClickListener(this);
button10.setOnClickListener(this);
button18.setOnClickListener(this);
button11.setOnClickListener(this);
button12.setOnClickListener(this);
button13.setOnClickListener(this);
button14.setOnClickListener(this);
button19.setOnClickListener(this);
button15.setOnClickListener(this);
button16.setOnClickListener(this);
button17.setOnClickListener(this);
button20.setOnClickListener(this);
}
public String stpu="";
public void onClick(View v) {
String st = " ";
int sum1 = 0;
TextView down = (TextView) findViewById(R.id.textView2);
while (v.getId() != R.id.button20) {
switch (v.getId()) {
case R.id.button:
st = st + 1;
down.setText(st);
break;
case R.id.button2:
st = st + 2;
down.setText(st);
break;
case R.id.button3:
st = st + 0;
down.setText(st);
break;
case R.id.button4:
st = st + 3;
down.setText(st);
break;
case R.id.button5:
st = st + 4
down.setText(st);
break;
case R.id.button6:
st = st + 5;
down.setText(st);
break;
case R.id.button7:
st = st + 6;
down.setText(st);
break;
case R.id.button8:
st = st + 7;
down.setText(st);
break;
case R.id.button9:
st = st + 8;
down.setText(st);
break;
case R.id.button10:
st = st + 9;
down.setText(st);
break;
case R.id.button14:
st = st + ".";
down.setText(st);
break;
case R.id.button15:
st = st + "+";
down.setText(st);
break;
case R.id.button16:
st = st + "-";
down.setText(st);
break;
case R.id.button17:
st = st + "*";
down.setText(st);
break;
case R.id.button18:
st = st + "/";
down.setText(st);
break;
case R.id.button19:
st = " ";
down.setText(st);
break;
default:
break;
}
}
}}
xml文件:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:id="@+id/button"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="66dp"
android:clickable="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:id="@+id/button2"
android:layout_alignTop="@+id/button"
android:layout_toRightOf="@+id/button"
android:layout_toEndOf="@+id/button"
android:clickable="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:id="@+id/button3"
android:layout_below="@+id/button2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="@+id/button2"
android:layout_alignEnd="@+id/button2"
android:clickable="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3"
android:id="@+id/button4"
android:layout_alignTop="@+id/button2"
android:layout_toRightOf="@+id/button2"
android:layout_toEndOf="@+id/button2"
android:clickable="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4"
android:id="@+id/button5"
android:layout_above="@+id/button"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:clickable="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5"
android:id="@+id/button6"
android:layout_above="@+id/button2"
android:layout_toLeftOf="@+id/button4"
android:layout_toStartOf="@+id/button4"
android:clickable="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6"
android:id="@+id/button7"
android:layout_alignTop="@+id/button6"
android:layout_alignLeft="@+id/button4"
android:layout_alignStart="@+id/button4"
android:clickable="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="7"
android:id="@+id/button8"
android:layout_above="@+id/button5"
android:layout_toLeftOf="@+id/button6"
android:layout_toStartOf="@+id/button6"
android:clickable="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="8"
android:id="@+id/button9"
android:layout_alignTop="@+id/button8"
android:layout_toLeftOf="@+id/button7"
android:layout_toStartOf="@+id/button7"
android:clickable="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="9"
android:id="@+id/button10"
android:layout_alignTop="@+id/button9"
android:layout_alignLeft="@+id/button7"
android:layout_alignStart="@+id/button7"
android:clickable="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="."
android:id="@+id/button14"
android:layout_above="@+id/button3"
android:layout_toRightOf="@+id/button4"
android:layout_toEndOf="@+id/button4"
android:clickable="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
android:id="@+id/button15"
android:layout_toRightOf="@+id/button7"
android:layout_toEndOf="@+id/button7"
android:layout_alignTop="@+id/button10"
android:layout_alignBottom="@+id/button7"
android:clickable="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"
android:id="@+id/button16"
android:layout_above="@+id/button10"
android:layout_alignLeft="@+id/button10"
android:layout_alignStart="@+id/button10"
android:layout_alignRight="@+id/button15"
android:layout_alignEnd="@+id/button15"
android:clickable="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="*"
android:id="@+id/button17"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="@+id/button10"
android:layout_above="@+id/button8"
android:clickable="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="/"
android:id="@+id/button18"
android:layout_above="@+id/button17"
android:layout_toLeftOf="@+id/button15"
android:layout_alignRight="@+id/button15"
android:layout_alignEnd="@+id/button15"
android:clickable="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="clear screen"
android:id="@+id/button19"
android:layout_above="@+id/button17"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="@+id/button18"
android:layout_toStartOf="@+id/button18"
android:clickable="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="="
android:id="@+id/button20"
android:layout_below="@+id/button4"
android:layout_alignLeft="@+id/button4"
android:layout_alignStart="@+id/button4"
android:layout_alignRight="@+id/button14"
android:layout_alignEnd="@+id/button14"
android:clickable="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/textView2"
android:layout_above="@+id/button18"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:clickable="true"
android:layout_alignParentTop="true" />
答案 0 :(得分:2)
因为你定义了双重函数public void onclick(View v)
,所以改变你的代码就像这样。
public void init(){
Button button = (Button) findViewById(R.id.button);
Button button2 = (Button) findViewById(R.id.button2);
Button button3 = (Button) findViewById(R.id.button3);
Button button4 = (Button) findViewById(R.id.button4);
Button button5 = (Button) findViewById(R.id.button5);
Button button6 = (Button) findViewById(R.id.button6);
Button button7 = (Button) findViewById(R.id.button7);
Button button8 = (Button) findViewById(R.id.button8);
Button button9 = (Button) findViewById(R.id.button9);
Button button10 = (Button) findViewById(R.id.button10);
Button button11 = (Button) findViewById(R.id.button11);
Button button12 = (Button) findViewById(R.id.button12);
Button button13 = (Button) findViewById(R.id.button13);
Button button14 = (Button) findViewById(R.id.button14);
Button button15 = (Button) findViewById(R.id.button15);
Button button16 = (Button) findViewById(R.id.button16);
Button button17 = (Button) findViewById(R.id.button17);
Button button18 = (Button) findViewById(R.id.button18);
Button button19 = (Button) findViewById(R.id.button19);
Button button20 = (Button) findViewById(R.id.button20);
button.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
button4.setOnClickListener(this);
button5.setOnClickListener(this);
button6.setOnClickListener(this);
button7.setOnClickListener(this);
button8.setOnClickListener(this);
button9.setOnClickListener(this);
button10.setOnClickListener(this);
button18.setOnClickListener(this);
button11.setOnClickListener(this);
button12.setOnClickListener(this);
button13.setOnClickListener(this);
button14.setOnClickListener(this);
button19.setOnClickListener(this);
button15.setOnClickListener(this);
button16.setOnClickListener(this);
button17.setOnClickListener(this);
button20.setOnClickListener(this);
}
并在onCreate
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
答案 1 :(得分:0)
与指定的@DanhDC一样,您应该创建按钮对象并在onCreate
中添加侦听器(或者像onCreate
中那样调用方法)。您还可以大大简化onClick
方法。编写xml文件的方式,没有必要进行切换。
的onCreate
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
Button button2 = (Button) findViewById(R.id.button2);
Button button3 = (Button) findViewById(R.id.button3);
Button button4 = (Button) findViewById(R.id.button4);
Button button5 = (Button) findViewById(R.id.button5);
Button button6 = (Button) findViewById(R.id.button6);
Button button7 = (Button) findViewById(R.id.button7);
Button button8 = (Button) findViewById(R.id.button8);
Button button9 = (Button) findViewById(R.id.button9);
Button button10 = (Button) findViewById(R.id.button10);
Button button11 = (Button) findViewById(R.id.button11);
Button button12 = (Button) findViewById(R.id.button12);
Button button13 = (Button) findViewById(R.id.button13);
Button button14 = (Button) findViewById(R.id.button14);
Button button15 = (Button) findViewById(R.id.button15);
Button button16 = (Button) findViewById(R.id.button16);
Button button17 = (Button) findViewById(R.id.button17);
Button button18 = (Button) findViewById(R.id.button18);
Button button19 = (Button) findViewById(R.id.button19);
Button button20 = (Button) findViewById(R.id.button20);
button.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
button4.setOnClickListener(this);
button5.setOnClickListener(this);
button6.setOnClickListener(this);
button7.setOnClickListener(this);
button8.setOnClickListener(this);
button9.setOnClickListener(this);
button10.setOnClickListener(this);
button18.setOnClickListener(this);
button11.setOnClickListener(this);
button12.setOnClickListener(this);
button13.setOnClickListener(this);
button14.setOnClickListener(this);
button19.setOnClickListener(this);
button15.setOnClickListener(this);
button16.setOnClickListener(this);
button17.setOnClickListener(this);
button20.setOnClickListener(this);
}
的onClick
public void onclick(View v) {
TextView down = (TextView) findViewById(R.id.textView2);
Button b = (Button)v;
if(b.getId() != R.id.button20){
down.setText(b.getText().toString());
}
}