查看对象ID

时间:2017-01-24 13:06:53

标签: android view

我在一个活动中有几个对象,我想在对它们执行任何操作之前检查它们的ID。但是我不确定该怎么做,我试过了:

 if (v.getId().contains("empty")){

但是这会出错,然后我尝试了

if (v.getId() == "[attr^=empty]"){

这也是错误的。说实话,我知道这两个都没有使用正确的作业等等,但我真的迷失了如何检查一个id是否包含某个子字符串......如果有人,我会很感激可以帮忙

PS:v是一个View对象

2 个答案:

答案 0 :(得分:0)

v.getId()返回一个整数。为了在做某事之前检查id,我建议使用以下内容:

switch (v.getId()){
     case R.id.ViewElement1:
          //do something here
          break;

     case R.id.ViewElement2:
          //do something here
          break;

     case R.id.ViewElement3:
          //do something here
          break;
}

这段代码的作用是获取View的Id然后通过switch-case块进行比较。如果从v.getId()获取id,则执行case块中的代码。 break;很重要,否则,执行的情况下的代码中的代码也会运行。如果更符合您的风格,您也可以为if-else执行此操作。

如果要将id作为字符串获取,可以尝试:

String ResourceIdAsString = v.getResources().getResourceName(v.getId());

然后你可以在String对象上执行if包含操作。

答案 1 :(得分:0)

我不完全确定你想要实现的目标。因此,我们会对id的工作原理进行一些概述。

首先,您必须id xml中的<TextView android:height="wrap_content" android:weight="match_parent" android:id="@+/myTextView" /> 所有观点,如:

switch(view.getId()){

  case R.id.myTextView1:

   break;

}

然后在您的JAVA代码中使用一个开关案例,如:

switch(view.getId()){

  case R.id.myTextView1:
  case R.id.myTextView2:
  case R.id.myTextView3:
  case R.id.myTextView4:
       perfomeSomeAction();
   break;

}

如果您有一些相同的观点,您可以使用它们:

#!/bin/bash

echo "Enter user name: " 
read username

echo "Transfer of $username profile started at " > ~/Desktop/transfer.log
date >> ~/Desktop/transfer.log
clear
echo "Transferring Data Folder to Documents Folder"
ditto -v /Volumes/C/Data ~/Documents/Data 2>> ~Desktop/transfer.log
echo "Data transfer complete"

echo "Transferring Documents Folder - Will error on system folders My Music, My Pictures, My Videos"
ditto -v /Volumes/C/Users/$username/Documents ~/Documents 2>> ~Desktop/transfer.log
echo "Documents transfer complete"

echo "Transferring Downloads Folder"
ditto -v /Volumes/C/Users/$username/Downloads ~/Documents/Downloads 2>> ~Desktop/transfer.log
echo "Downloads transfer complete"

echo "Transferring Favorites Folder to Documents Folder"
ditto -v /Volumes/mpc/Users/$username/Favorites ~/Documents/Favorites 2>> ~Desktop/transfer.log
echo "Favorites transfer complete"

echo "Transferring Desktop Folder"
ditto -v /Volumes/C/Users/$username/Desktop ~/Desktop 2>> ~Desktop/transfer.log
echo "Desktop transfer complete"

echo "Transferring Music Folder"
ditto -v /Volumes/C/Users/$username/Music ~/Music 2>> ~Desktop/transfer.log
echo "Music transfer complete"

echo "Transferring Pictures Folder"
ditto -v /Volumes/C/Users/$username/Pictures ~/Documents/Pictures 2>> ~Desktop/transfer.log
echo "Pictures transfer complete"

echo "Transferring Videos Folder"
ditto -v /Volumes/C/Users/$username/Videos ~/Videos 2>> ~Desktop/transfer.log
echo "Videos transfer complete"

echo "Transferring PST from AppData to Documents Folder"
cp -v /Volumes/C/Users/$username/AppData/Local/Microsoft/Outlook/*.pst ~/Documents/Outlook Files 2>> ~Desktop/transfer.log
echo "AppData transfer complete"

echo "Removing My Music, My Pictures, My Videos"
rm -r "~/Documents/My Music"
rm -r "~/Documents/My Pictures"
rm -r "~/Documents/My Videos"
echo "Removal complete"

echo "Transfer Complete - Press any key to exit"
read Exit
exit