多个spinner + edittext到文本视图(android)

时间:2014-10-15 20:54:10

标签: java android android-edittext textview android-spinner

所以我是一个非常初学的android编程和我试图做非常基本的东西。我有6个微调器,旁边有编辑文本(数字),我有一个按钮。单击按钮时,我需要加载带有文本视图的新页面,并在文本视图中显示微调器+编辑文本的内容,例如:

spinner1:blue edittext:3 spinner2:orange edittext2:9

会显示

3:蓝色 9:橙色

我不知道该怎么做,我是否需要创建一个新的活动页面?或者如何做?请问是否需要任何代码。

谢谢

1 个答案:

答案 0 :(得分:0)

你必须从基础开始学习,有很多教程可以帮助你学习基本的东西。在这里,我给你一些想法,我希望它会给你一个良好的开端。

首先,您必须创建两个活动及其布局(第一个用于用户输入,第二个用于显示)。

用户输入的布局(layout_spinners.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<Spinner
    android:id="@+id/spnr_day"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<Spinner
    android:id="@+id/spnr_month"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />


<EditText
    android:id="@+id/et_text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<Button
    android:id="@+id/btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button" />

 </LinearLayout>

显示布局(layout_result.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical" >

<TextView
    android:id="@+id/result"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

</LinearLayout>

活动(SpinnerDemo.java)

public class SpinnerDemo extends Activity {
String[] days = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
        "Friday", "Saturday" };
String[] months = { "January", "February", "March", "April", "May" };

Spinner daySpinner, monthSpinner;
ArrayAdapter<String> daysAdapter, monthAdapter;
EditText editText;
Button button;
String selectedDay, selectedMonth, text;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_spinners);

    // Instantiating views
    daySpinner = (Spinner) findViewById(R.id.spnr_day);
    monthSpinner = (Spinner) findViewById(R.id.spnr_month);
    editText = (EditText) findViewById(R.id.et_text);
    button = (Button) findViewById(R.id.btn);

    // Instantiating adapter
    daysAdapter = new ArrayAdapter<String>(SpinnerDemo.this,
            android.R.layout.simple_dropdown_item_1line, days);
    monthAdapter = new ArrayAdapter<String>(SpinnerDemo.this,
            android.R.layout.simple_dropdown_item_1line, months);

    // Setting adapter to Spinner
    daySpinner.setAdapter(daysAdapter);
    monthSpinner.setAdapter(monthAdapter);

    // OnClickListener of Button
    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            // Getting values of text and spinners
            selectedDay = daySpinner.getSelectedItem().toString();
            selectedMonth = monthSpinner.getSelectedItem().toString();
            text = editText.getText().toString();

            // Creating an Intent to open new Activity(Screen) and sending
            // the details
            Intent intent = new Intent(SpinnerDemo.this, Results.class);
            intent.putExtra("day", selectedDay);
            intent.putExtra("month", selectedMonth);
            intent.putExtra("text", text);
            startActivity(intent);

        }
    });

}

}

活动(Results.java)

public class Results extends Activity {

TextView textView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    setContentView(R.layout.layout_result);

    textView = (TextView) findViewById(R.id.result);

    Intent intent = getIntent();
    if(intent!=null)
    {
        String day = intent.getStringExtra("day");
        String month = intent.getStringExtra("month");
        String text = intent.getStringExtra("text");

        textView.setText(day + "\n" + month + "\n" + text);

    }
    else
    {
        textView.setText("Intent is null");
    }
}
}