Android的微调改变的setContentView [英] Android spinner to change setContentView

查看:88
本文介绍了Android的微调改变的setContentView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有两个不同的选项迄今好手。我试图做到的是,如果选择第一微调选项,然后我的setContentView到一个特定的布局和执行对应于布局code。如果第二微调选项选择也是一样。我知道我需要使用setOnItemSelectedListener在一定程度上,但我不知道如何做到这一点正是工作。下面是我想要在编码方面做一个快速模拟了

  spinner.setonItemSelectedListener(本);
    如果(微调=第一微调选项){
         的setContentView(R.layout.lay1);
         //其他code在这里
    } ELSEIF(微调=第二微调选项){
         的setContentView(R.layout.lay2);
         //其他code在这里
    }

我知道语法是坏在这里,我只是试图让怎么可以这样做一个大致的了解。

编辑:@ codeMagic
这是我的code是如何设置迄今。 '项目'只是一个字符串数组有2个元素。

  ArrayAdapter<串GT;适配器=新ArrayAdapter<串GT;(这一点,
            R.layout.my_spinner_style,物品){            公共查看getView(INT位置,查看convertView,父母的ViewGroup){
                视图V = super.getView(位置,convertView,父母);
                字体TF = Typeface.createFromAsset(getAssets(),字体/ EXO-Bold.otf);
                ((的TextView)V).setTypeface(TF);
                返回伏;
            }
            公共查看getDropDownView(INT位置,查看convertView,父母的ViewGroup){
                视图V = super.getDropDownView(位置,convertView,父母);
                字体TF = Typeface.createFromAsset(getAssets(),字体/ EXO-Bold.otf);
                ((的TextView)V).setTypeface(TF);
                //v.setBackgroundColor(Color.GREEN);                返回伏;
            }
    };
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    gasChoice.setAdapter(适配器);    addListenerOnSpinnerItemSelection();
}公共无效addListenerOnSpinnerItemSelection(){
    gasChoice =(微调)findViewById(R.id.gasChoice);
    gasChoice.setOnItemSelectedListener(新OnItemSelected());}


解决方案

我不知道究竟是什么问题您有,但其pretty接近。你只需要添加方法

  @覆盖
公共无效onItemSelected(适配器视图<>为arg0,ARG1观,诠释ARG2,长ARG3){
     TextView的电视=(TextView中)ARG1; //获取TextView的选择
     字符串文本= tv.getText()的toString()。
     如果(text.equals(FirstText)){//比较框中的文本
          的setContentView(R.layout.lay1);
     //其他code在这里
     } ELSEIF(text.equals(FirstText)){
          的setContentView(R.layout.lay2);
         //其他code在这里
     }
}

有这样做,例如获取位置( ARG2 ),并比较了一下在你的适配器,但因为我的不同的方法不知道你是怎么做的任何这一点,这就是让你开始最简单的方法。

So, I have a spinner with two different options thus far. What I am trying to accomplish is, if "First Spinner Option" is chosen, then I setContentView to a specific layout and execute code corresponding to that layout. The same goes if "Second Spinner Option" is chosen. I know that I need to use setOnItemSelectedListener to a certain extent, but I am not sure how this would work exactly. Below is a quick mock up of what I'm trying to do in coding terms

spinner.setonItemSelectedListener(this);
    if(spinner = first spinner option){
         setContentView(R.layout.lay1);
         //other code here
    }elseif(spinner = second spinner option){
         setContentView(R.layout.lay2);
         //other code here
    }

I know the syntax is bad here, Im just trying to get a general idea of how this could be done.

EDIT: @CodeMagic This is how my code is setup thus far. 'items' is just an array of strings with 2 elements.

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            R.layout.my_spinner_style, items) {

            public View getView(int position, View convertView, ViewGroup parent) {
                View v = super.getView(position, convertView, parent);
                Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/Exo-Bold.otf");
                ((TextView) v).setTypeface(tf);
                return v;
            }


            public View getDropDownView(int position,  View convertView,  ViewGroup parent) {
                View v =super.getDropDownView(position, convertView, parent);
                Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/Exo-Bold.otf");
                ((TextView) v).setTypeface(tf);
                //v.setBackgroundColor(Color.GREEN);

                return v;
            }
    };


    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);                                 
    gasChoice.setAdapter(adapter);  

    addListenerOnSpinnerItemSelection();
}

public void addListenerOnSpinnerItemSelection(){
    gasChoice = (Spinner) findViewById(R.id.gasChoice);
    gasChoice.setOnItemSelectedListener(new OnItemSelected());

}

解决方案

I don't know exactly what the problem is you are having but its pretty close. You just need to add the method

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
     TextView tv = (TextView)arg1;  // get the TextView selected
     String text = tv.getText().toString();
     if(text.equals("FirstText")){   // compare the text in the box
          setContentView(R.layout.lay1);
     //other code here
     }elseif(text.equals("FirstText")){
          setContentView(R.layout.lay2);
         //other code here
     }
}

There are different ways of doing this such as getting the position (arg2) and comparing that to what's in your adapter but since I don't know how you are doing any of that, this is the easiest way to get you started.

这篇关于Android的微调改变的setContentView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆