我正在开发我的第二个Android应用程序,一个简单的计算器 通过以下操作
加法,减法,乘法,除法,模数
代码:
XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center">
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:textAppearanceMedium"
android:text="Number 1"
android:id="@+id/textView"
android:layout_column="1"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="@+id/num1"
android:layout_column="3"
/>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Number 2"
android:id="@+id/textView2"
android:layout_column="1" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="@+id/num2"
android:layout_column="3" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+ADD"
android:id="@+id/btAdd"
android:layout_column="3" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-Subtract"
android:id="@+id/btSub"
android:layout_column="3" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="xMultiply"
android:id="@+id/btMul"
android:layout_column="3" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="/ Divide"
android:id="@+id/btDiv"
android:layout_column="3" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="% Mod"
android:id="@+id/btMod"
android:layout_column="3" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"></TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Result:"
android:id="@+id/result"
android:layout_column="1" />
</TableRow>
</TableLayout>
</RelativeLayout>
java代码是:
mainActivity.java
package in.infoexpo.sreerajm.secapp;
import android.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment(){
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.fragment_main, container, false);
Button btAdd = (Button) rootView.findViewById(R.id.btAdd);
Button btSub = (Button) rootView.findViewById(R.id.btSub);
Button btMul = (Button) rootView.findViewById(R.id.btMul);
Button btDiv = (Button) rootView.findViewById(R.id.btDiv);
Button btMod = (Button) rootView.findViewById(R.id.btMod);
btAdd.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
int n1, n2;
EditText num1 = (EditText) rootView.findViewById(R.id.num1);
EditText num2 = (EditText) rootView.findViewById(R.id.num2);
n1 = Integer.parseInt(num1.getText().toString());
n2 = Integer.parseInt(num2.getText().toString());
TextView result = (TextView) rootView.findViewById(R.id.result);
result.setText(n1 + n2);
}
});
btSub.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
int n1, n2;
EditText num1 = (EditText) rootView.findViewById(R.id.num1);
EditText num2 = (EditText) rootView.findViewById(R.id.num2);
n1 = Integer.parseInt(num1.getText().toString());
n2 = Integer.parseInt(num2.getText().toString());
TextView result = (TextView) rootView.findViewById(R.id.result);
result.setText(n1 - n2);
}
});
btMul.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
int n1, n2;
EditText num1 = (EditText) rootView.findViewById(R.id.num1);
EditText num2 = (EditText) rootView.findViewById(R.id.num2);
n1 = Integer.parseInt(num1.getText().toString());
n2 = Integer.parseInt(num2.getText().toString());
TextView result = (TextView) rootView.findViewById(R.id.result);
result.setText(n1 * n2);
}
});
btDiv.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
int n1, n2;
EditText num1 = (EditText) rootView.findViewById(R.id.num1);
EditText num2 = (EditText) rootView.findViewById(R.id.num2);
n1 = Integer.parseInt(num1.getText().toStenter code herering());
n2 = Integer.parseInt(num2.getText().toString());
TextView result = (TextView) rootView.findViewById(R.id.result);
result.setText(n1 / n2);
}
});
btMod.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
int n1, n2;
EditText num1 = (EditText) rootView.findViewById(R.id.num1);
EditText num2 = (EditText) rootView.findViewById(R.id.num2);
n1 = Integer.parseInt(num1.getText().toString());
n2 = Integer.parseInt(num2.getText().toString());
TextView result = (TextView) rootView.findViewById(R.id.result);
result.setText(n1 % n2);
}
});
return rootView;
}
}
}
我尝试了很多更改,但无法删除一个错误 错误:找不到符号变量fragment_main Plz帮我修复错误。
答案 0 :(得分:1)
你有可能把xml?它必须命名为 fragment_main.xml 并放在文件夹 res / layout
下