spinner(下拉列表框),RadioButton(单选按钮),EditText,Adapter的应用

来源:互联网 发布:海尔的大数据营销案例 编辑:程序博客网 时间:2024/06/07 03:58

1、其布局文件为:

<?xml version="1.0" encoding="utf-8"?><TableLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"     android:id="@+id/modetype"    android:stretchColumns="1">    <TableRow>        <TextView            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:gravity="right"            android:padding="10dip"            android:text="@string/nettype"            android:textStyle="bold" />        <Spinner            android:id="@+id/spinner_1"            android:layout_width="fill_parent"            android:layout_height="match_parent"            android:drawSelectorOnTop="false"                 android:entries="@array/type" />        </TableRow>        <TableRow>            <TextView                android:gravity="right"                android:padding="10dip"                android:text="@string/netmode"                android:textStyle="bold" />            <Spinner                android:id="@+id/spinner_2"                android:layout_width="fill_parent"                android:layout_height="match_parent"                android:drawSelectorOnTop="false"               <!--android:entries=""属性是给ArrayAdapter赋值的另一种方法,在利用代码来赋值时可以不适用此属性-->                android:entries="@array/type" />                 <!-- 为什么不能再添加新的源,item值呐? -->        </TableRow>                 <!-- radiogroup是控制单选的 -->            <RadioGroup                android:id="@+id/menu"                android:layout_width="fill_parent"                android:layout_height="wrap_content"                android:checkedButton="@+id/lunch"                android:orientation="horizontal" >                <RadioButton                    android:id="@+id/autoip"                    android:text="@string/autoip" />                <RadioButton                    android:id="@+id/staticip"                    android:text="@string/staticip" />            </RadioGroup>                  <TableRow>           <TextView android:text="@string/ip"           android:gravity="right"           android:padding="3dip"           android:textStyle="bold"           />        <EditText             android:id="@+id/ipaddress"            android:padding="3dip"            android:scrollHorizontally="true"            android:hint="@null"/>            </TableRow>            <TableRow>           <TextView android:text="@string/mask"           android:gravity="right"           android:padding="3dip"           android:textStyle="bold"/>        <EditText             android:id="@+id/maskaddress"            android:padding="3dip"            android:scrollHorizontally="true"            android:hint="@null"/>            </TableRow>            <TableRow>           <TextView android:text="@string/gateway"           android:gravity="right"           android:padding="3dip"           android:textStyle="bold"/>        <EditText             android:id="@+id/gatewayaddress"            android:padding="3dip"            android:scrollHorizontally="true"            android:hint="@null"/>            </TableRow>             <TableRow>           <TextView android:text="@string/dns1"           android:gravity="right"           android:padding="3dip"           android:textStyle="bold"/>        <EditText             android:id="@+id/dns1address"            android:padding="3dip"            android:scrollHorizontally="true"            android:hint="@null"/>            </TableRow>            <TableRow>           <TextView android:text="@string/dns2"           android:gravity="right"           android:padding="3dip"                      android:textStyle="bold"/>        <EditText             android:id="@+id/dns2address"            android:padding="3dip"            android:scrollHorizontally="true"            android:hint="@null"/>         </TableRow>         <TableRow >          <TextView android:text="@string/save"           android:gravity="right"           android:padding="3dip"           android:textStyle="bold"/>          <Button              android:id="@+id/save"              android:layout_width="wrap_content"              android:layout_height="wrap_content"              android:text="@string/bn" />           </TableRow><TableRow ><TextView android:text="@string/clear"           android:gravity="right"           android:padding="3dip"           android:textStyle="bold"/><Button    android:id="@+id/clear"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="@string/clear" /> </TableRow>         <TableRow >    <TextView android:text="@string/display"           android:gravity="right"           android:padding="3dip"           android:textStyle="bold"/>            <EditText                android:id="@+id/display"                android:layout_height="wrap_content"                android:ems="10"                android:hint="@null"                android:padding="3dip"                android:scrollHorizontally="true" >            </EditText></TableRow><TableRow android:gravity="right">         <Button             android:id="@+id/close"             android:layout_width="wrap_content"             android:layout_height="wrap_content"   android:text="@string/close" /></TableRow></TableLayout>
2、布局界面展示:

3、代码如下:

import java.util.ArrayList;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.AdapterView;import android.widget.ArrayAdapter;import android.widget.Button;import android.widget.EditText;import android.widget.RadioButton;import android.widget.RadioGroup;import android.widget.Spinner;import android.widget.Toast;import com.cslc.android.easypos.SystemControl;//学习ArrayListpublic class setNetworkModeTest extends Activity {private static final String TAG = "setNetworkMode";SystemControl sc=new SystemControl(this);private static String typeNumber,modeNumber;private static int type,mode;private static EditText display,ipaddress,maskaddress,gatewayaddress,dns1address,dns2address;private static RadioButton autoip,staticip;private static Spinner spinner_c,spinner_d;private static ArrayAdapter<String> aspnNetType,aspnnetmode;private static ArrayList<String> allnettype,allnetmode;private static final String[] mNetType={"4","1","2","3"};//为什么不能放到局部方法中?private static final String[] mNetMode={"0","1","2"};//为什么不能放到局部方法中?protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.snm_layout);display=(EditText)findViewById(R.id.display);ipaddress=(EditText)findViewById(R.id.ipaddress);maskaddress=(EditText)findViewById(R.id.maskaddress);gatewayaddress=(EditText)findViewById(R.id.gatewayaddress);dns2address=(EditText)findViewById(R.id.dns2address);dns1address=(EditText)findViewById(R.id.dns1address);autoip=(RadioButton)findViewById(R.id.autoip);staticip=(RadioButton)findViewById(R.id.staticip);Button save=(Button)findViewById(R.id.save);Button close=(Button)findViewById(R.id.close);Button clear=(Button)findViewById(R.id.clear);//----------------------------------------------------------spinner--------------------------------------------------------------------find_and_modify_view();//获取spinner选择的值,设置监听事件spinner_c.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {    @Override    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {    /*arg0   The AdapterView where the selection happened   解析:我们可以假单的看为下拉框(当我们为下拉框添加这个监听器时可以这么做),其他情况类似    *   arg1   The view within the AdapterView that was clicked  对应的就是ArrayAdapter构造函数中的第二个参数R.layout.list_item    *arg2    The position of the view in the adapter    第一项position=0,第二项position=1...    *arg3   The row id of the item that is selected    *    */    Log.e("eee",""+arg3);    typeNumber = mNetType[arg2];    //设置显示当前选择的项    arg0.setVisibility(View.VISIBLE);    //display.setText(cardNumber);//获取选择的值    type=Integer.valueOf(typeNumber);    }@Overridepublic void onNothingSelected(AdapterView<?> arg0) {// TODO Auto-generated method stub}});spinner_d.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {    @Override    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {    modeNumber = mNetType[arg2];    //设置显示当前选择的项    arg0.setVisibility(View.VISIBLE);   // display.setText(cardNumber);//获取选择的值    mode=Integer.valueOf(modeNumber);    }@Overridepublic void onNothingSelected(AdapterView<?> arg0) {// TODO Auto-generated method stub}});   //cardNumber = spinner_c.getSelectedItem().toString();      //display.setText(cardNumber);//获取选择的值//---------------------------------------------------------------RadioButton-----------------------------------------------------------/*autoip.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {  //错在:用的RadioButton点的事件,应该用RadioGroup点事件                        public void onCheckedChanged(RadioGroup group, int checkedId) {                  // TODO Auto-generated method stub                  String string = "";                  if(checkedId == autoip.getId()){                      string = autoip.getText().toString();                  }else if(checkedId == staticip.getId()){                      string = staticip.getText().toString();                  }                  display.setText(string);              }        });  */RadioGroup menu = (RadioGroup)findViewById(R.id.menu);//必须要用RadioGroupmenu.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){@Overridepublic void onCheckedChanged(RadioGroup arg0, int checkedId) {// TODO Auto-generated method stubString string = "";                  if(checkedId == autoip.getId()){                  {//--------------------------------------------EditText  setEnabled(false),不可编辑状态----------------------------------                ipaddress.setFocusable(false);            ipaddress.setEnabled(false);            maskaddress.setFocusable(false);            maskaddress.setEnabled(false);            gatewayaddress.setFocusable(false);            gatewayaddress.setEnabled(false);            dns1address.setFocusable(false);            dns1address.setEnabled(false);            dns2address.setFocusable(false);            dns2address.setEnabled(false);                //display.setText("unedit");                              }                }                else if(checkedId == staticip.getId())                { 
                             //--------------------------------------------EditText  setEnabled(false),可编辑状态----------------------------------
ipaddress.setFocusableInTouchMode(true); ipaddress.setFocusable(true);//clearFocus(); ipaddress.setEnabled(true); ipaddress.requestFocus(); maskaddress.setFocusableInTouchMode(true); maskaddress.setFocusable(true); maskaddress.setEnabled(true); maskaddress.requestFocus(); gatewayaddress.setFocusableInTouchMode(true); gatewayaddress.setFocusable(true); gatewayaddress.setEnabled(true); gatewayaddress.requestFocus(); dns1address.setFocusableInTouchMode(true); dns1address.setFocusable(true); dns1address.setEnabled(true); dns1address.requestFocus(); dns2address.setFocusableInTouchMode(true); dns2address.setFocusable(true); dns2address.setEnabled(true); dns2address.requestFocus(); //display.setText("edit"); } }});//-----------------------------------------clear EditText------------------------------------------------------------clear.setOnClickListener(new OnClickListener(){public void onClick(View source){ipaddress.setText("");maskaddress.setText("");gatewayaddress.setText("");dns1address.setText("");dns2address.setText("");}});close.setOnClickListener(new OnClickListener(){public void onClick(View source){Intent intent=new Intent(setNetworkModeTest.this,scActivity.class);startActivity(intent);finish();//end of current activity}});save.setOnClickListener(new OnClickListener(){public void onClick(View source){//---------------------------------------------------多个string放入Intent中-----------------------------------------------Intent intent=new Intent();if(mode==2){ intent.putExtra("ip", ipaddress.getText().toString()); intent.putExtra("mask", maskaddress.getText().toString()); intent.putExtra("gateway", gatewayaddress.getText().toString()); intent.putExtra("dns1", dns1address.getText().toString()); intent.putExtra("dns2", dns2address.getText().toString()); Toast.makeText(setNetworkModeTest.this, "保存成功", Toast.LENGTH_SHORT).show();boolean flag=sc.setNetworkMode(type,mode,intent);if(flag)display.setText("true"); else display.setText("false");}else if(mode==1){Log.d(TAG,"mode is 1");if(sc.setNetworkMode(type,mode,intent))display.setText("true"); else display.setText("false");//display.setText("true");}}});}//------------------------------------------------给spinner设置Adapter-----------------------------------------------------------------------public void find_and_modify_view(){spinner_c=(Spinner)findViewById(R.id.spinner_1);spinner_d=(Spinner)findViewById(R.id.spinner_2); allnettype = new ArrayList<String>();allnetmode = new ArrayList<String>();for(int i=0;i<mNetType.length;i++){allnettype.add(mNetType[i]);}for(int i=0;i<mNetMode.length;i++){allnetmode.add(mNetMode[i]);}aspnNetType= new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,allnettype);aspnnetmode= new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,allnetmode);//aspnNetType.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);// 设定下拉菜单的样式 spinner_c.setAdapter(aspnNetType);spinner_d.setAdapter(aspnnetmode);}/** * 设置EditText是否可编辑 * @author com.tiantian * @param editText 要设置的EditText * @param value 可编辑:true 不可编辑:false * editText.setEnabled(true);可编辑 editText.setEnabled(false);不可编辑 */ }




0 0