Eclipse中的文本字段和旋转器

时间:2014-07-13 17:46:52

标签: java android eclipse spinner textfield

如何在Eclipse中设置文本字段以与Spinner中选择的项目一起使用?示例 - 如果用户选择第二个项目(在下拉菜单中可以说3个),我希望他们能够使用文本字段来控制微调器,以便为所选项目设置值。把它想象成一个翻译工具。如果该人从语言菜单中选择英语,然后键入英语单词(在文本字段中),我希望它能够将其转换为他们在第二个微调器(西班牙语)中选择的任何内容“按钮。

我很抱歉,如果这个问题似乎更需要java / eclipse课程然后代码帮助,但我感谢任何帮助:)

MainActivity

package com.overworldinnovations.datatool;

import java.util.ArrayList;
import java.util.List;

import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.os.Build;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {


private Spinner spinner1, spinner2;
private Button buttonConvert;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    addItemsOnSpinner2();
    addListenerOnButton();
    addListenerOnSpinnerItemSelection();

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();   
    }
}

public void addItemsOnSpinner2() {

    spinner2 = (Spinner) findViewById(R.id.spinner2);
    //List<String> list = new ArrayList<String>();
    spinner2 = (Spinner) findViewById(R.id.spinner2);
    spinner2.setOnItemSelectedListener(new CustomOnItemSelectedListener());

    /*list.add("list 1");
    list.add("list 2");
    list.add("list 3");
    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
        android.R.layout.simple_spinner_item, list);
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner2.setAdapter(dataAdapter);*/
  }

public void addListenerOnSpinnerItemSelection() {
    spinner1 = (Spinner) findViewById(R.id.spinner1);
    spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener());
  }

public void addListenerOnButton() {

    spinner1 = (Spinner) findViewById(R.id.spinner1);
    spinner2 = (Spinner) findViewById(R.id.spinner2);
    buttonConvert = (Button) findViewById(R.id.buttonConvert);

    buttonConvert.setOnClickListener(new OnClickListener() {

      @Override
      public void onClick(View v) {

        Toast.makeText(MainActivity.this,
        "OnClickListener : " + 
                "\nSpinner 1 : "+ String.valueOf(spinner1.getSelectedItem()) + 
                "\nSpinner 2 : "+ String.valueOf(spinner2.getSelectedItem()),
            Toast.LENGTH_SHORT).show();
      }

    });
}
@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;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container,
                false);
        return rootView;
    }
}

}

activity_main

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.overworldinnovations.datatool.MainActivity"
tools:ignore="MergeRootFrame" >

<Button
    android:id="@+id/buttonConvert"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="50dp"
    android:text="Convert" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:text="Please Select A Data Type To Be Converted"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<Spinner
    android:id="@+id/spinner2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/spinner1"
    android:layout_marginTop="45dp"
    android:entries="@array/type_arrays"
    android:prompt="@string/data_prompt" />

<Spinner
    android:id="@+id/spinner1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/textView1"
    android:layout_marginTop="60dp"
    android:entries="@array/type_arrays"
    android:prompt="@string/data_prompt" />

<EditText
    android:id="@+id/editText2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/editText1"
    android:layout_below="@+id/spinner2"
    android:layout_marginTop="22dp"
    android:ems="10"
    android:inputType="numberSigned" >

    <requestFocus />
</EditText>

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/spinner1"
    android:layout_centerHorizontal="true"
    android:ems="10"
    android:inputType="numberSigned" />

</RelativeLayout>

的字符串

<?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="app_name">Data Tool</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="convert">Convert</string>
<string name="data_tool_is_an_application_that_converts_binary_to_decimal_d">Data Tool is an application that converts Binary to Decimal :D</string>
<string name ="data_prompt">Choose a data type</string>

<string-array name = "type_arrays">
    <item >Decimal</item>
    <item >Binary</item>
    <item >Hexidecimal</item>
</string-array>
</resources>

CustomOnItemSelectedListener

package com.overworldinnovations.datatool;

import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Toast;

 public class CustomOnItemSelectedListener implements OnItemSelectedListener {

  public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
    Toast.makeText(parent.getContext(), 
        "OnItemSelectedListener : " + parent.getItemAtPosition(pos).toString(),
        Toast.LENGTH_SHORT).show();
   }

   @Override
   public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub
   }

}

2 个答案:

答案 0 :(得分:0)

好吧,我无法告诉你关于你的转换的事情,但我会告诉你如何根据选定的微调器条目和如何操作文本字段来做一些事情。其余的你需要编码: - )

这是一个实现微调器的类,它根据微调器中的选定选项执行某些操作。它也可以变得非常不同,但这应该可以很容易地解释。

public  class MyClass extends Fragment implements AdapterView.OnItemSelectedListener {

// Your Spinner
public Spinner spinner;

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    final View view = inflater.inflate(R.layout.tab2, container, false);

    // Customize from here
    final TextView textview = (TextView) view.findViewById(R.id.textView);
    final Button button = (Button) view.findViewById(R.id.btn_calculate);

    setSpinnerContent( view );

    // attach an OnClickListener
    button.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {

            Integer selectedOption = spinner.getSelectedItemPosition();


                if (selectedOption==0) {

                    myString = "Option 1 selected";
                }
                if (selectedOption==1) {

                    myString = "Option 2 selected";
                }
                if (selectedOption==2) {

                    myString = "Option 3 selected";
                }

            textview.setText(myString);
            }


        }
    );
    return view;
}
private void setSpinnerContent( View view )
{
    spinner = (Spinner) view.findViewById( R.id.spinner );
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(), R.array.planets_array, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter( adapter );

}


public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) { // ItemsSelected

    //An alternate way to listen to the selected items
}

public void onNothingSelected(AdapterView<?> parent) {
    // Another interface callback
}

}

微调器从String XML资源获取内容,如下所示:

<string-array name="planets_array">
    <item>Euro</item>
    <item>US-Dollar</item>
    <item>Yen</item>
</string-array>

答案 1 :(得分:0)

    spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

        public void onItemSelected(AdapterView<?> parent, View view,
                int position, long id) {
            String val = spinner.getSelectedItem().toString();//Getting Value of Selected Item
            txtPerc.setText(val);//TextView
        }

        public void onNothingSelected(AdapterView<?> parent) {
            // TODO Auto-generated method stub

        }
    });

做这样的事情..请清楚你的问题。 希望它的帮助..