2 momo1199 Momo1199 于 2013.06.28 13:45 提问

从编辑文本中检索int值

有两个文本框和一个数组,想要将数组索引号写在文字框1,然后在点击Calculate按钮时,在文本框2中获取数组值。

"MainActivity.java"代码如下:

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button B1 = (Button) findViewById(R.id.button1);
B1.setOnClickListener(new View.OnClickListener() {

final EditText ET2 = (EditText) findViewById(R.id.editText2);

public void onClick(View v) {

final int[] oil={20,30,40,50,60,70,80,90,100};
final String str = String.valueOf(R.id.editText1);
final int y = Integer.parseInt(str);
final int z = oil[y];

ET2.setText(z);
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}

"activity_main.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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="67dp"
    android:ems="10" 
    android:inputType="number" />

<EditText
    android:id="@+id/editText2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/editText1"
    android:layout_below="@+id/editText1"
    android:ems="10"
    android:inputType="number" >

    <requestFocus />
</EditText>

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="Calculate" />

2个回答

cj6585256
cj6585256   2013.06.28 13:57
已采纳

大概就是这样,你写的错误太多了

 private Button b1;
    private EditText e1,e2;
    private final int[] oil={20,30,40,50,60,70,80,90,100};
    public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    b1 = (Button) findViewById(R.id.button1);
    e1 = (EditText) findViewById(R.id.editText1);
    e2 = (EditText) findViewById(R.id.editText2);
    b1 .setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
    String string = e1.getText().toString();
    int index = Integer.valueOf(string);
    int z = oil[index ];
    e2.setText(z);//这里不能直接这样写 会报错 传递int值代表的是id
    应该写成e2.setText(""+z);或者e2.setText(String.valueOf(z));
    }
    });
    }
loFataMer
loFataMer   2013.06.28 15:24
       EditText searchtext = (EditText) findViewById(R.id.searchtext);

        String copy = searchtext.getText().toString());

获取:

 EditText etSearch = (EditText) findViewById(R.id.searchtext2);
 etSearch.setText(copy);
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!