- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 Android 中创建了一个包含日期、月份和年份的 Spinner
。我想将 Spinner
中的数据显示到 String
中,格式为 dd/mm/yyyy
。但它总是包含错误:
public static int compare(int lhs, int rhs) {
return lhs < rhs ? -1 : (lhs == rhs ? 0 : 1);
}
private static NumberFormatException invalidInt(String s) {
throw new NumberFormatException("Invalid int: \"" + s + "\"");
}
我的部分 XML 布局:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Spinner
android:id="@+id/datePayment"
android:layout_width="75dp"
android:layout_height="44dp"
android:layout_marginRight="20dp"
android:background="#FFFFFF" />
<Spinner
android:id="@+id/monthPayment"
android:layout_width="100dp"
android:layout_height="46dp"
android:layout_marginRight="20dp"
android:background="#FFFFFF" />
<Spinner
android:id="@+id/yearPayment"
android:layout_width="60dp"
android:layout_height="48dp"
android:background="#FFFFFF" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:layout_marginTop="10dp">
<Button
android:id="@+id/bsubmit"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:gravity="center"
android:background="#6b6b6b"
android:text="Submit"
android:textColor="#ffffff" />
<Button
android:id="@+id/breset"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textColor="#ffffff"
android:background="#6b6b6b"
android:text="Reset"/>
</LinearLayout>
代码
public class Payment extends Activity implements OnItemSelectedListener
{
private Spinner spDate, spMonth, spYear;
private static final String[] date = {"01","02","03","04","05","06","07","08","09",
"10","11","12","13","14","15","16","17","18","19","20","21","22","23","24",
"25","26","27","28","29","30","31"};
private static final String[] month = {"Januari", "Februari", "Maret", "April",
"Mei", "Juni", "Juli", "Agustus", "September",
"Oktober", "November", "Desember"};
private static final String[] year = {"2014", "2015"};
private Button bSubmit;
String tampspDate, tampspMonth, tampspYear, numberMonth, tampNumberMonth;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.payment);
spDate = (Spinner)findViewById(R.id.datePayment);
spMonth = (Spinner)findViewById(R.id.monthPayment);
spYear = (Spinner)findViewById(R.id.yearPayment);
ArrayAdapter<String> AdaptDate = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, date);
AdaptDate.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spDate.setAdapter(AdaptDate);
ArrayAdapter<String> AdaptMonth = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, month);
AdaptMonth.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spMonth.setAdapter(AdaptMonth);
ArrayAdapter<String> AdaptYear = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, year);
AdaptYear.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spYear.setAdapter(AdaptYear);
bSubmit = (Button)findViewById(R.id.bsubmit);
spDealer.setOnItemSelectedListener(this);
spDate.setOnItemSelectedListener(this);
spMonth.setOnItemSelectedListener(this);
spYear.setOnItemSelectedListener(this);
bSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
tampTanggal = ""+tampspDate+ " "+tampNumberMonth+" "+tampspYear;
Toast.makeText(Payment.this, tampTanggal, Toast.LENGTH_SHORT).show();
}
});
}
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
tampspDate = spDate.getSelectedItem().toString();
tampspMonth = spMonth.getSelectedItem().toString();
tampspYear = spYear.getSelectedItem().toString();
tampNumberMonth = numMonth(tampspMonth);
}
public String numMonth(String abc){
if(abc.equals("Januari")){
numberMonth="01";
}
else if(abc.equals("Februari")){
numberMonth="02";
}
else if(abc.equals("Maret")){
numberMonth="03";
}
else if(abc.equals("April")){
numberMonth="04";
}
else if(abc.equals("Mei")){
numberMonth="05";
}
else if(abc.equals("Juni")){
numberMonth="06";
}
else if(abc.equals("Juli")){
numberMonth="07";
}
else if(abc.equals("Agustus")){
numberMonth="08";
}
else if(abc.equals("September")){
numberMonth="09";
}
else if(abc.equals("Oktober")){
numberMonth="10";
}
else if(abc.equals("November")){
numberMonth="11";
}
else if(abc.equals("Desember")){
numberMonth="12";
}
return numberMonth;
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
}
最佳答案
下面的代码可以帮助您获取所需格式的日期。
final String[] month = {"Januari", "Februari", "Maret", "April", "Mei", "Juni",
"Juli", "Agustus", "September", "Oktober", "November", "Desember"};
final String[] date = {"01","02","03","04","05","06","07","08","09","10","11","12","13","14","15",
"16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31"};
final String[] year = {"2014", "2015"};
// This method identifies the month from the array
// Use this function instead of numMonth function in your original code.
// Its easier to maintain.
public int getMonth(String passedMonth) {
for (int i = 0; i < month.length; i++) {
// Returns the array index of the passedMonth from month Array
if (month[i].equals(passedMonth)) {
return (i+1);
}
}
return (-1); // Invalid month
}
// This method would get the input from your spinner and print the date as
// per your desired format.
public void printDateInMyFormat (String date, String month, String year)
{
// This is the format you are requesting for.
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
String dateInString = date +"/"+ getMonth(month) +"/"+ year;
System.out.println("dateInString : "+dateInString);
try {
Date dt = formatter.parse(dateInString);
System.out.println("My Date ::"+dt);
System.out.println("My Required Date String :: " +formatter.format(dt));
} catch (ParseException e) {
e.printStackTrace();
}
}
结果:
Input:
test.printDateInMyFormat ("01", "September", "2015");
输出:
Month is : 9
dateInString : 01/9/2015
My Date :: Tue Sep 01 00:00:00 IST 2015
My Required Date String :: 01/09/2015
代码处理无效日期如下:
Input:
test.printDateInMyFormat ("31", "September", "2015");
输出:
Month is : 9
dateInString : 31/9/2015
My Date :: Thu Oct 01 00:00:00 IST 2015
My Required Date String :: 01/10/2015
关于android - 将 Spinner 转换为字符串中的日期格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25497569/
所以我有一个微调器,我成功地更改了所选项目的颜色,但我无法更改下拉菜单中项目的颜色 这是我的 spinner_layout.xml 这是我的 样式.xml @style/Spin
我需要做的是,如果选择了微调器 1 中的某个项目,它需要在微调器 01 中显示某个数组例如如果微调器一个选定的项目是红色微调器 01 需要显示 level_array 作为微调器 01 的下拉选项,否
我想在点击微调器外部后关闭 Android 微调器。这可能吗? 最佳答案 我在这方面运气不错,即使它不能完全奏效。 Spinner 适配器的获取 View : public View getView(
我正在使用 eneter 框架来处理我的 android 应用程序中的通信;问题是当我试图填充微调器时,将适配器设置为微调器会导致未定义的异常 这里是代码 public void populateSp
我在 xml 文件中有一个微调器,宽度和高度设置为 wrap_content,我将背景设置为微调器, 文本居中显示,如果我在 dp 或 fill_parent 中设置宽度,则文本显示为左对齐,但当微调
我有一个微调器 onItemSelect 我需要根据第一个选择打开另一个微调器。这是代码......我能够给第一个微调器充气但是在选择一个条目时没有任何反应 Spinner filterSpinner
当我打开 TalkBack 时,在微调器中做出选择后,焦点会立即返回到屏幕顶部(工具栏)。我希望在用户选择后立即将焦点放在微调器上。 我已经尝试将微调器设置为可聚焦: spinner.setFocus
我知道已经有大约一百万个主题,但请听我说完。 标题说明了一切,当我在微调器 1 中选择一个项目时,微调器 2 会获得一个特定的选项列表供您选择(然后将用于显示信息)。它本质上是一本小型通讯录。 *更新
我正在为 Android 构建一个有两个微调器的应用程序。第一个有一系列选择。我有一些代码可以更改第二个微调器的值,但数组在第二个微调器中永远不会改变。我检查过,selectedValue 确实等于所
我有一个 xml 布局文件,其中包含一些小部件,包括一个 Spinner 我想在微调器中显示一个字符串列表,该列表是在运行时作为函数的结果生成的,因此它不能在 arrays.xml 中。 我试过: S
spinner 没有根据另一个 spinner selection 填充,我已经研究了好几个小时了,仍然无法解决问题,没有错误洛格卡特。提前致谢。 这是我的代码 ArrayAdapter adapt
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 6 年前。 Improve this qu
我介绍一个Spinner在我的小部件中,每次我从中选择不同的值时,我都想执行一些操作。 是否可以? 我好像只收到事件 on_press和 on_release ,但在做出不同值(value)的选择时,
这是一个应用程序运行中的简单 gif,以显示我的意思:Video Gif here 我有一个 Spinner,这是我的 XML 代码: 我遵循了一些教程并浏览了此处,以使用 parse.com 动态
我有一个包含 1 个微调器的“发票”登记 Activity 。在这个 Spinner 中包含带有 IDCard 的卡片注册、标题和描述(保存在数据库的“Card”表中)。当我将此“发票”记录保存在“发
我在我的元素中实现了 mat-spinner,但它只有很少的可配置更改,例如颜色、旋转模式。我想在微调器中包含品牌/公司 Logo 的图像图标,我该如何实现? 以下是 Stackblitz 链接: h
在处理 Android Honeycomb 项目时,我偶然发现了一个有趣的问题。如下图所示,在对话框中展开 Spinner 时,底部的导航栏与它重叠。因此,无法选择底部的元素。 为了解决这个问题,我尝
当我在第一个微调器中选择中国作为国家/地区时,看到我想要的,所以我希望第二个微调器必须显示中国的所有州,这是通过我的代码完成的……但是……!我的查询是,当我从第二个微调器中选择状态时,它会自动将其设置
我正在尝试为应用创建一个简单的设置页面。 我想创建一个对象列表,我在 XML 文件中定义了其布局。 在这些对象的布局中有一个标题 TextView、一个帮助 TextView 和一个 Spinner。
我正面临这个问题,已经阅读了很多问题/答案但无法解决。 我有一个带有微调器的 ListView(用户必须为该行中的产品选择一个数量),但是当项目多于 View 并且在微调器中选择一个数字后滚动 Lis
我是一名优秀的程序员,十分优秀!