gpt4 book ai didi

java - Android 中如何从字符串中获取值

转载 作者:太空宇宙 更新时间:2023-11-04 13:09:35 25 4
gpt4 key购买 nike

我开始编写 Android 应用程序,但陷入了需要从字符串获取值的部分。目前正在尝试制作 Android 版计算器。在此示例中,我尝试获取仅用于添加两个值的值。

添加我的代码:

MainActivity.java

public class MainActivity extends AppCompatActivity {

@Bind({R.id.button0, R.id.button1, R.id.button2, R.id.button3, R.id.button4, R.id.button5, R.id.button6, R.id.button7, R.id.button8,
R.id.button9, R.id.buttonDot, R.id.buttonC, R.id.buttonDeleteLastSymbol, R.id.buttonMultiplication, R.id.buttonDivision, R.id.buttonSubtraction, R.id.buttonEqual})
List<Button> button;
@Bind(R.id.editText1)
EditText editText;

public String sign = "";
public Double mathDouble, mathDouble2;

static final ButterKnife.Action<View> DISABLE = new ButterKnife.Action<View>() {
@Override
public void apply(View view, int index) {
view.setEnabled(false);
}
};
static final ButterKnife.Setter<View, Boolean> ENABLED = new ButterKnife.Setter<View, Boolean>() {
@Override
public void set(View view, Boolean value, int index) {
view.setEnabled(value);
}
};

//Buttons 0-9
@OnClick({R.id.button0, R.id.button1, R.id.button2, R.id.button3, R.id.button4, R.id.button5, R.id.button6, R.id.button7, R.id.button8,
R.id.button9, R.id.buttonDot})
void inputValue(Button button) {
editText.append(button.getText());
}

@OnClick({R.id.buttonPlus, R.id.buttonSubtraction, R.id.buttonDivision, R.id.buttonMultiplication})
void inputSign(Button button) {
sign = button.getText().toString();
editText.append(sign);
}

public Double FirstValue() {
String writtenText = editText.getText().toString();
String firstValue = writtenText.substring(0, writtenText.indexOf("+"));

return mathDouble = Double.parseDouble(firstValue);
}

Double SecondValue() {
String writtenText = editText.getText().toString();
String secondValue = writtenText.substring(writtenText.indexOf("+") + 1, writtenText.charAt(writtenText.length() - 1));
return mathDouble2 = Double.parseDouble(secondValue);
}

//Buttons Equal, CE and C
@OnClick(R.id.buttonC)
void WriteLeftBracket() {
editText.setText(String.valueOf(""));
}

@OnClick(R.id.buttonDeleteLastSymbol)
void WriteRightBracket() {
if (editText.getText().toString().length() > 0) {
editText.setText(editText.getText().toString().substring(0, editText.getText().toString().length() - 1));
editText.setSelection(editText.getText().length());
}
}

@OnClick(R.id.buttonEqual)
void Equal(Button button) {
editText.append(button.getText());
editText.setText(Double.toString(mathDouble + mathDouble2));
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ButterKnife.bind(this);
}
}

content_main.xml

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="@layout/activity_main">

<LinearLayout
android:id="@+id/layout1"
android:layout_width="match_parent"
android:layout_height="60dp">

<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:singleLine="true"
android:lines="1"
/>

</LinearLayout>

<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/layout1">

<Button
android:id="@+id/buttonC"
android:layout_width="80dp"
android:layout_height="80dp"
android:text="@string/erase"
android:textSize="30sp" />

<Button
android:id="@+id/buttonDeleteLastSymbol"
android:layout_width="80dp"
android:layout_height="80dp"
android:text="@string/delete"
android:textStyle="bold"
android:textSize="30sp" />

<Button
android:id="@+id/buttonMultiplication"
android:layout_width="80dp"
android:layout_height="80dp"
android:text="@string/multiplication"
android:textSize="30sp" />

</LinearLayout>

<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/linearLayout1">

<Button
android:id="@+id/button7"
android:layout_width="80dp"
android:layout_height="80dp"
android:layoutDirection="inherit"
android:text="@string/seven"
android:textSize="30sp" />

<Button
android:id="@+id/button8"
android:layout_width="80dp"
android:layout_height="80dp"
android:layoutDirection="inherit"
android:text="@string/eight"
android:textSize="30sp" />

<Button
android:id="@+id/button9"
android:layout_width="80dp"
android:layout_height="80dp"
android:layoutDirection="inherit"
android:text="@string/nine"
android:textSize="30sp" />
</LinearLayout>

<LinearLayout
android:id="@+id/linearLayout3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/linearLayout2">

<Button
android:id="@+id/button4"
android:layout_width="80dp"
android:layout_height="80dp"
android:layoutDirection="inherit"
android:text="@string/four"
android:textSize="30sp" />

<Button
android:id="@+id/button5"
android:layout_width="80dp"
android:layout_height="80dp"
android:layoutDirection="inherit"
android:text="@string/five"
android:textSize="30sp" />

<Button
android:id="@+id/button6"
android:layout_width="80dp"
android:layout_height="80dp"
android:layoutDirection="inherit"
android:text="@string/six"
android:textSize="30sp" />
</LinearLayout>

<LinearLayout
android:id="@+id/linearLayout4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/linearLayout3">

<Button
android:id="@+id/button1"
android:layout_width="80dp"
android:layout_height="80dp"
android:layoutDirection="inherit"
android:text="@string/one"
android:textSize="30sp" />

<Button
android:id="@+id/button2"
android:layout_width="80dp"
android:layout_height="80dp"
android:layoutDirection="inherit"
android:text="@string/two"
android:textSize="30sp" />

<Button
android:id="@+id/button3"
android:layout_width="80dp"
android:layout_height="80dp"
android:layoutDirection="inherit"
android:text="@string/three"
android:textSize="30sp" />
</LinearLayout>

<LinearLayout
android:id="@+id/linearLayout5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/linearLayout4">

<Button
android:id="@+id/button0"
android:layout_width="160dp"
android:layout_height="80dp"
android:layoutDirection="inherit"
android:text="@string/zero"
android:textSize="30sp" />

<Button
android:id="@+id/buttonDot"
android:layout_width="80dp"
android:layout_height="80dp"
android:layoutDirection="inherit"
android:text="@string/dot"
android:textSize="30sp" />
</LinearLayout>

<LinearLayout
android:id="@+id/linearLayout6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/layout1"
android:layout_toRightOf="@id/linearLayout1">

<Button
android:id="@+id/buttonDivision"
android:layout_width="80dp"
android:layout_height="80dp"
android:layoutDirection="inherit"
android:text="@string/division"
android:textSize="30sp" />

</LinearLayout>

<Button
android:id="@+id/buttonSubtraction"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_below="@+id/linearLayout1"
android:layout_toEndOf="@+id/linearLayout1"
android:text="@string/subtraction"
android:textSize="30sp" />

<Button
android:id="@+id/buttonEqual"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_below="@+id/linearLayout2"
android:layout_toEndOf="@+id/linearLayout1"
android:text="@string/equal"
android:textSize="30sp" />
<Button
android:id="@+id/buttonPlus"
android:layout_width="80dp"
android:layout_height="160dp"
android:layout_below="@+id/linearLayout3"
android:layout_toEndOf="@+id/linearLayout1"
android:text="@string/plus"
android:textSize="30sp"

/>

</RelativeLayout>

strings.xml

    <resources>
<string name="app_name">Calculator</string>
<string name="action_settings">Settings</string>
<string name="dot">.</string>
<string name="plus">+</string>
<string name="subtraction">-</string>
<string name="division">/</string>
<string name="multiplication">*</string>
<string name="equal">=</string>
<string name="erase">C</string>
<string name="delete">←</string>

//Numbers
<string name="one">1</string>
<string name="two">2</string>
<string name="three">3</string>
<string name="four">4</string>
<string name="five">5</string>
<string name="six">6</string>
<string name="seven">7</string>
<string name="eight">8</string>
<string name="nine">9</string>
<string name="zero">0</string>
<string name="illegal_argument_exeption">Cannot Divide By Zero!</string>
</resources>

最佳答案

用它来获取值(value):

getResources().getString("R.string.StringName");

例如

String zero = getResources().getString("R.string.zero");

关于java - Android 中如何从字符串中获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34110794/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com