如何为一个 activity 设置两个 xml?如果我 select 一个选项,我必须得到一个 xml,反之亦然?
How to set two xml's for one activity?If i select one option i have to get one xml and vice versa?
如果我 select "New customer" 在 customertype spinner(spn_cust) 中,我必须显示一个包含 button,Spinner,Edittext,textview.If i [=24= 的视图] "Existing Customer" 我必须得到另一个包含 Button,Edittext 的视图。
这怎么可能?我应该隐藏一个 xml 而 select 一个选项吗?我将如何以编程方式解决这个问题?
pncustomertype.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int position, long arg3) {
//String item = spncustomertype.getItemAtPosition(position).toString();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
有很多可用的解决方案:
- 最简单:
findViewbyID
和 setVisibility
到您的按钮
- 创建不同的 xml 布局并将它们与
include
标签组合,setVisibility
事件。
- 创建不同的xml布局,使用
LayoutInflater
膨胀每个xml文件并通过代码 添加
如果与元素相关的操作(即按钮、编辑文本等)根据所选选项(新客户或现有客户)而不同,最好创建两个不同的视图 类 View1(包含按钮,Spinner,Edittext,Textview)和View2(包含Button,Edittext)。
public class View1 extends RelativeLayout{
//put your code and use
//View view = layoutInflater.inflate(R.layout.YOUR_LAYOUT, this); to inflate your layout
}
public class View2 extends RelativeLayout{
//put your code and use
//View view = layoutInflater.inflate(R.layout.YOUR_LAYOUT, this); to inflate your layout
}
现在将这些视图添加到视图组(例如 Linearlayout、RelativeLayout 等),
viewgroup.addView(View1/View2) 取决于您的选择(新客户或现有客户)。
You can do it by setting visibilities in xml and code.
<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:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
android:paddingBottom="16dp"
tools:context=".MainActivity">
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/spinner"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:id="@+id/button"
android:layout_below="@+id/spinner"
android:layout_alignParentStart="true"
android:visibility="gone"/>
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/spinner2"
android:layout_below="@+id/button"
android:layout_alignParentStart="true"
android:visibility="gone"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:text="edit_Text"
android:layout_below="@+id/spinner2"
android:layout_alignParentStart="true"
android:visibility="gone"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/textView"
android:layout_centerVertical="true"
android:layout_alignParentStart="true"
android:visibility="gone"/>
</RelativeLayout>
package com.example.user.spinnerapp;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends Activity{
private Button button;
private Spinner spinner;
private EditText editText;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final List<String> spinnerArray = new ArrayList<>();
spinnerArray.add("New Customer");
spinnerArray.add("Existing Customer");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, spinnerArray);
// adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
final Spinner sItems = (Spinner) findViewById(R.id.spinner);
button = (Button)findViewById(R.id.button);
spinner = (Spinner)findViewById(R.id.spinner2);
editText = (EditText)findViewById(R.id.editText);
textView = (TextView)findViewById(R.id.textView);
sItems.setAdapter(adapter);
sItems.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//String selected = spinnerArray.get(position);
String selected = sItems.getSelectedItem().toString();
if (selected.equals("New Customer")) {
button.setVisibility(View.VISIBLE);
spinner.setVisibility(View.VISIBLE);
editText.setVisibility(View.VISIBLE);
textView.setVisibility(View.VISIBLE);
}
else if(selected.equals("Existing Customer")){
spinner.setVisibility(View.INVISIBLE);
textView.setVisibility(View.INVISIBLE);
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
设置2xmls
使用必填字段创建 xml 并根据您的条件扩充它们。
如果我 select "New customer" 在 customertype spinner(spn_cust) 中,我必须显示一个包含 button,Spinner,Edittext,textview.If i [=24= 的视图] "Existing Customer" 我必须得到另一个包含 Button,Edittext 的视图。 这怎么可能?我应该隐藏一个 xml 而 select 一个选项吗?我将如何以编程方式解决这个问题?
pncustomertype.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int position, long arg3) {
//String item = spncustomertype.getItemAtPosition(position).toString();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
有很多可用的解决方案:
- 最简单:
findViewbyID
和setVisibility
到您的按钮 - 创建不同的 xml 布局并将它们与
include
标签组合,setVisibility
事件。 - 创建不同的xml布局,使用
LayoutInflater
膨胀每个xml文件并通过代码 添加
如果与元素相关的操作(即按钮、编辑文本等)根据所选选项(新客户或现有客户)而不同,最好创建两个不同的视图 类 View1(包含按钮,Spinner,Edittext,Textview)和View2(包含Button,Edittext)。
public class View1 extends RelativeLayout{
//put your code and use
//View view = layoutInflater.inflate(R.layout.YOUR_LAYOUT, this); to inflate your layout
}
public class View2 extends RelativeLayout{
//put your code and use
//View view = layoutInflater.inflate(R.layout.YOUR_LAYOUT, this); to inflate your layout
}
现在将这些视图添加到视图组(例如 Linearlayout、RelativeLayout 等), viewgroup.addView(View1/View2) 取决于您的选择(新客户或现有客户)。
You can do it by setting visibilities in xml and code.
<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:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
android:paddingBottom="16dp"
tools:context=".MainActivity">
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/spinner"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:id="@+id/button"
android:layout_below="@+id/spinner"
android:layout_alignParentStart="true"
android:visibility="gone"/>
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/spinner2"
android:layout_below="@+id/button"
android:layout_alignParentStart="true"
android:visibility="gone"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:text="edit_Text"
android:layout_below="@+id/spinner2"
android:layout_alignParentStart="true"
android:visibility="gone"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/textView"
android:layout_centerVertical="true"
android:layout_alignParentStart="true"
android:visibility="gone"/>
</RelativeLayout>
package com.example.user.spinnerapp;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends Activity{
private Button button;
private Spinner spinner;
private EditText editText;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final List<String> spinnerArray = new ArrayList<>();
spinnerArray.add("New Customer");
spinnerArray.add("Existing Customer");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, spinnerArray);
// adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
final Spinner sItems = (Spinner) findViewById(R.id.spinner);
button = (Button)findViewById(R.id.button);
spinner = (Spinner)findViewById(R.id.spinner2);
editText = (EditText)findViewById(R.id.editText);
textView = (TextView)findViewById(R.id.textView);
sItems.setAdapter(adapter);
sItems.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//String selected = spinnerArray.get(position);
String selected = sItems.getSelectedItem().toString();
if (selected.equals("New Customer")) {
button.setVisibility(View.VISIBLE);
spinner.setVisibility(View.VISIBLE);
editText.setVisibility(View.VISIBLE);
textView.setVisibility(View.VISIBLE);
}
else if(selected.equals("Existing Customer")){
spinner.setVisibility(View.INVISIBLE);
textView.setVisibility(View.INVISIBLE);
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
设置2xmls 使用必填字段创建 xml 并根据您的条件扩充它们。