你好朋友,今天我的问题是关于string_array。在我的活动中,我创建了两个按钮。现在,在(strings.xml)上,我创建了包含两个项目的字符串数组名称,并插入了一些文本,如下所示。在每个按钮上单击我都会我想分别访问每个项目。例如,在button1上单击“显示我第一项”,在按钮2上单击“给我第二项”。您能给我按钮单击的代码吗,因为我对如何访问该项目一无所知。已经创建了一个textview活动来显示每次click时都显示我的项目。到目前为止,我的代码运行良好,但是我需要其他必须添加的额外代码的帮助。请帮助我。
// activity page
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</ScrollView>
<Button
android:layout_height="wrap_content"
android:layout_marginTop="90dp"
android:layout_marginEnd="60dp"
android:layout_marginRight="50dp"
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:text="@string/OKILA"
android:textSize="18sp"
android:textStyle="bold" />
<Button
android:id="@+id/button1"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_marginTop="160dp"
android:layout_marginRight="50dp"
android:text="1"
android:textSize="18sp"
android:textStyle="bold" />
</RelativeLayout>
//strings.xml
<string-array name="chapters">
<item>this is tes1</item>
<item>this is test2</item>
</string-array>
//main
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import org.w3c.dom.Text;
public class Lipok extends AppCompatActivity implements View.OnClickListener {
Toolbar mActionBarToolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lipok);
mActionBarToolbar=(Toolbar)findViewById(R.id.CHAPTERS);
setSupportActionBar(mActionBarToolbar);
getSupportActionBar().setTitle("CHAPTERS");
String[] chapters=getResources().getStringArray(R.array.chapters);
}
@Override
public void onClick(View v) {
String text="";
switch(v.getId()){
case R.id.btn1 : {
text = getResources().getStringArray(R.array.chapters)[0];
break;
}
case R.id.button1 : {
text = getResources().getStringArray(R.array.chapters)[1];
break;
}
}
Intent intent = new Intent(Lipok.this,lipokchapters.class);
intent.putExtra("DATA", text);
startActivity(intent);
}
}
这是Android的基础知识,因此在回答之前,建议您先使用在Stackoberflow上发布之前,请先阅读基础教程。
尝试一下: