Java Android应用程序:更改为抽屉布局后,calc功能不起作用
作为DroperLayout的子级,我将应用程序中的布局从RelativeLayout更改为RelativeLayout。除了Java Android应用程序:更改为抽屉布局后,calc功能不起作用,java,android,Java,Android,作为DroperLayout的子级,我将应用程序中的布局从RelativeLayout更改为RelativeLayout。除了MainActivity.java中的onClick()中的所有内容之外,所有内容都可以正常工作。按下按钮button1后,计算结果为之前工作的NaN。这与布局的改变有关。我不知道是什么导致了这个问题。 也许有人能帮我? GitHub: MainActivity.java package com.lob.gradescalculator; import android.
MainActivity.java
中的onClick()
中的所有内容之外,所有内容都可以正常工作。按下按钮button1
后,计算结果为之前工作的NaN
。这与布局的改变有关。我不知道是什么导致了这个问题。
也许有人能帮我?
GitHub:
MainActivity.java
package com.lob.gradescalculator;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import android.view.MenuInflater;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
DrawerLayout drawerLayoutgesamt;
ActionBarDrawerToggle drawerToggle;
int sum = 0;
int ects = 0;
float mark = 0;
NumberFormat numberFormat = new DecimalFormat("0.00");
Button button1;
TextView textView1;
EditText editText1;
EditText editText2;
EditText editText3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar1);
setSupportActionBar(toolbar);
drawerLayoutgesamt = (DrawerLayout) findViewById(R.id.drawerlayoutgesamt);
drawerToggle = new ActionBarDrawerToggle(MainActivity.this, drawerLayoutgesamt, R.string.auf, R.string.zu);
drawerLayoutgesamt.setDrawerListener(drawerToggle);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
drawerToggle.syncState();
button1 = (Button)findViewById(R.id.button1);
textView1 = (TextView)findViewById(R.id.textView1);
editText1 = (EditText)findViewById(R.id.editTextGDI);
editText2 = (EditText)findViewById(R.id.editTextGDW);
editText3 = (EditText)findViewById(R.id.editTextProgrammieren1);
loadSavedPreferences();
button1.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v){
ViewGroup group = (ViewGroup)findViewById(R.id.activity_main);
for (int i = 0, count = group.getChildCount(); i < count; ++i) {
View view = group.getChildAt(i);
if (view instanceof EditText) {
if(view.equals(editText1) && !(editText1.getText().toString().matches(""))){ //GDI
System.out.println("edittext1");
ects += 5;
try{ sum += Integer.parseInt(editText1.getText().toString()) *5; } catch(NumberFormatException n){}
}
else if(view.equals(editText2)&& !(editText2.getText().toString().matches(""))){ //GDW
System.out.println("edittext2");
ects += 5;
try{ sum += Integer.parseInt(editText2.getText().toString()) *5; } catch(NumberFormatException n){}
}
else if(view.equals(editText3)&& !(editText3.getText().toString().matches(""))){
System.out.println("edittext3");
ects += 7;
try{ sum += Integer.parseInt(editText3.getText().toString()) *7; } catch(NumberFormatException n){}
}
}
}
mark = (float)sum / (float)ects;
textView1.setText(String.valueOf(numberFormat.format(mark)));
savePreferences("editText1", editText1.getText().toString());
savePreferences("editText2", editText2.getText().toString());
savePreferences("editText3", editText3.getText().toString());
sum = 0;
ects = 0;
mark = 0;
}
}
);
}
private void loadSavedPreferences() {
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(this);
editText1.setText(sharedPreferences.getString("editText1", ""));
editText2.setText(sharedPreferences.getString("editText2", ""));
editText3.setText(sharedPreferences.getString("editText3", ""));
}
private void savePreferences(String key, String value) {
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(key, value);
editor.commit();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
int id = item.getItemId();
if(id == R.id.action_settings){
return true;
}
if(drawerToggle.onOptionsItemSelected(item)){
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onPostCreate(@Nullable Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
drawerToggle.onConfigurationChanged(new Configuration());
}
}
package com.lob.gradescalculator;
导入android.content.SharedReferences;
导入android.content.res.Configuration;
导入android.preference.PreferenceManager;
导入android.support.annotation.Nullable;
导入android.support.v4.widget.DrawerLayout;
导入android.support.v7.app.ActionBarDrawerToggle;
导入android.support.v7.app.AppActivity;
导入android.os.Bundle;
导入android.support.v7.widget.Toolbar;
导入android.view.Menu;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.TextView;
导入java.text.DecimalFormat;
导入java.text.NumberFormat;
导入android.view.MenuInflater;
导入android.view.MenuItem;
公共类MainActivity扩展了AppCompatActivity{
工具栏;
抽屉布局抽屉布局金额;
ActionBarDrawerToggle抽屉切换;
整数和=0;
int ects=0;
浮点数=0;
NumberFormat NumberFormat=新的十进制格式(“0.00”);
按钮1;
文本视图文本视图1;
编辑文本编辑文本1;
编辑文本编辑文本2;
编辑文本编辑文本3;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar=(toolbar)findViewById(R.id.toolbar1);
设置支持操作栏(工具栏);
付款人付款金额=(付款人付款金额)findViewById(R.id.Droperlayoutgesamt);
drawerToggle=newactionBarDrawerToggle(MainActivity.this、drawerLayoutgesamt、R.string.auf、R.string.zu);
付款人付款金额设置付款人付款方式(付款人转帐);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
drawerToggle.syncState();
button1=(按钮)findViewById(R.id.button1);
textView1=(TextView)findViewById(R.id.textView1);
editText1=(EditText)findViewById(R.id.editTextGDI);
editText2=(EditText)findViewById(R.id.editTextGDW);
editText3=(EditText)findViewById(R.id.editTextProgrammieren1);
loadSavedReferences();
button1.setOnClickListener(
新建按钮。OnClickListener(){
公共void onClick(视图v){
ViewGroup group=(ViewGroup)findViewById(R.id.activity\u main);
for(int i=0,count=group.getChildCount();i<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.lob.gradescalculator.MainActivity">
<android.support.v4.widget.DrawerLayout
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:id="@+id/drawerlayoutgesamt"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true">
<!-- Activity Layout für Hauptbereich -->
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/activitylayout"
>
<android.support.v7.widget.Toolbar
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/toolbar1"
android:background="#c6d9ff"
android:fitsSystemWindows="true"
>
</android.support.v7.widget.Toolbar>
<!-- TextViews -->
<TextView
android:id="@+id/WIF_Title"
android:text="Wirtschaftsinformatik B.Sc."
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25dp"
android:layout_x="23dp"
android:layout_y="200dp"
android:layout_below="@+id/toolbar1"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"/>
<TextView
android:id="@+id/textViewGDW"
android:text="Grundlagen der Wirtschaftsinformatik"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
android:textStyle="normal|bold"
android:textAlignment="textStart"
android:layout_below="@+id/textViewGDI"
android:layout_marginTop="30dp"
android:layout_x="45dp"
android:layout_y="344dp"
android:layout_alignStart="@+id/textViewGDI" />
<TextView
android:id="@+id/TextViewProgrammieren1"
android:text="Programmieren 1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
android:textStyle="normal|bold"
android:textAlignment="textStart"
android:layout_below="@+id/textViewGDW"
android:layout_marginTop="32dp"
android:layout_x="113dp"
android:layout_y="238dp"
android:layout_alignStart="@+id/textViewGDW" />
<!-- EditTexts -->
<!-- Buttons -->
<EditText
android:id="@+id/editTextGDI"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_below="@+id/WIF_Title"
android:layout_alignParentEnd="true"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:inputType="number"
android:textColorHint="@drawable/selector"
android:background="@drawable/roundedborders"
android:digits="12345"
android:maxLength="1"
/>
<TextView
android:id="@+id/textViewGDI"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Grundlagen der Informatik"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
android:textStyle="normal|bold"
android:textAlignment="center"
android:layout_x="125dp"
android:layout_y="184dp"
android:layout_below="@+id/WIF_Title"
android:layout_alignParentStart="true"
android:layout_marginStart="3dp"
android:layout_marginTop="15dp"/>
<EditText
android:id="@+id/editTextGDW"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:textColorHint="@drawable/selector"
android:background="@drawable/roundedborders"
android:ems="10"
android:layout_x="69dp"
android:layout_y="91dp"
android:layout_alignBaseline="@+id/textViewGDW"
android:layout_alignBottom="@+id/textViewGDW"
android:layout_alignStart="@+id/editTextGDI"
android:layout_alignEnd="@+id/editTextGDI" />
<EditText
android:id="@+id/editTextProgrammieren1"
android:layout_height="wrap_content"
android:inputType="number"
android:textColorHint="@drawable/selector"
android:background="@drawable/roundedborders"
android:ems="10"
android:layout_x="78dp"
android:layout_y="264dp"
android:layout_width="50dp"
android:layout_alignBaseline="@+id/TextViewProgrammieren1"
android:layout_alignBottom="@+id/TextViewProgrammieren1"
android:layout_alignStart="@+id/editTextGDW"
android:layout_alignEnd="@+id/editTextGDW" />
<TextView
android:id="@+id/textView1"
android:text="..."
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="71dp"
android:layout_x="23dp"
android:layout_y="200dp"
android:layout_above="@+id/button1"
android:layout_centerHorizontal="true" />
<Button
android:id="@+id/button1"
android:text="Berechne"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="75dp"
android:layout_x="128dp"
android:layout_y="442dp"
android:layout_alignParentBottom="true"
android:layout_alignEnd="@+id/textViewGDW" />
</RelativeLayout>
<!-- Drawerlayout für links -->
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/drayerlayoutsingle"
android:layout_gravity="start"
android:background="#fff">
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>