SORRY問題已解決!如何在活動中顯示對話框中的edittext輸入
剛纔忘了指出,在對話框中編輯文本:
number1 = (EditText)dialogcalc.findViewById(R.id.number1);
number2 = (EditText)dialogcalc.findViewById(R.id.number2);
我試圖做出乘以來自用戶的數量的計算器。 該數字可以放在對話框中的Edittext中。 點擊對話框上的按鈕計算結果後,該數字必須乘以數字,並顯示在激活對話框的活動中。 當我點擊計算按鈕時出現錯誤,我做錯了什麼?
的main.xml(對話)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!-- number 1 -->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="number:" />
<EditText
android:id="@+id/number1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="0"
android:inputType="number"
android:layout_marginLeft="20dp" />
</LinearLayout>
<!-- number 2 -->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="number:" />
<EditText
android:id="@+id/number2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="0"
android:inputType="number"
android:layout_marginLeft="20dp" />
</LinearLayout>
<Button
android:id="@+id/btncalculate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Calculate"/>
<Button
android:id="@+id/btnreset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reset" />
</LinearLayout>
Java文件:
package com.tip.calc;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
public class OverviewpageActivity extends Activity {
private TextView multiplydisplay;
private TextView multiply2display;
private Button btntocalculator;
private Button btnpopup;
private EditText number1;
private EditText number2;
private double number1calc = 0;
private double number2calc = 0;
private double multiply = 0;
private double multiply2 = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.overview);
multiplydisplay = (TextView)findViewById(R.id.multiplydisplay);
multiply2display = (TextView)findViewById(R.id.multiplydisplay2);
btntocalculator = (Button)findViewById(R.id.btntocalculator);
btnpopup = (Button)findViewById(R.id.btnpopup);
btntocalculator.setOnClickListener(new Button.OnClickListener() {
public void onClick (View v) {
final Dialog dialogcalc = new Dialog(OverviewpageActivity.this);
dialogcalc.setContentView(R.layout.main);
dialogcalc.setTitle("calc");
dialogcalc.setCancelable(true);
Button buttoncalculate = (Button) dialogcalc.findViewById(R.id.btncalculate);
buttoncalculate.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(number1.getText().toString().trim().length() < 1){number1calc=0;}
else{number1calc=Double.parseDouble(number1.getText().toString());}
if(number2.getText().toString().trim().length() < 1){number2calc=0;}
else{number2calc=Double.parseDouble(number2.getText().toString());}
//calculate
multiply=(number1calc*number2calc)*0.222;
multiply2=(number1calc*number2calc)*0.888;
//display
multiplydisplay.setText(Double.toString(multiply));
multiply2display.setText(Double.toString(multiply2));
dialogcalc.dismiss();
}
});
dialogcalc.show();
}
});
btnpopup.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//set up dialog
final Dialog dialog = new Dialog(OverviewpageActivity.this);
dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("This is my custom dialog box");
dialog.setCancelable(true);
TextView text = (TextView) dialog.findViewById(R.id.TextView01);
text.setText(R.string.about);
ImageView img = (ImageView) dialog.findViewById(R.id.ImageView01);
img.setImageResource(R.drawable.carbon);
Button button = (Button) dialog.findViewById(R.id.Button01);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
});
}
}
什麼是錯誤? – 2012-01-30 11:09:39