【发布时间】:2018-04-24 12:54:33
【问题描述】:
我是 Android Studio 的新手,有人可以帮我使用 Spinner 制作图像吗?
感谢您的提示和建议。
【问题讨论】:
-
请编辑您的问题并详细解释“使用 Spinner 生成图像”是什么意思。
标签: android imageview spinner android-spinner android-studio-2.3
我是 Android Studio 的新手,有人可以帮我使用 Spinner 制作图像吗?
感谢您的提示和建议。
【问题讨论】:
标签: android imageview spinner android-spinner android-studio-2.3
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.Layout;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CheckedTextView;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
public class MainActivity extends AppCompatActivity implements TextWatcher, View.OnClickListener, AdapterView.OnItemSelectedListener, CompoundButton.OnCheckedChangeListener {
EditText ed;
CheckBox c;
Spinner s1, s2;
ToggleButton t;
LinearLayout l;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed = (EditText) findViewById(R.id.editText);
ed.addTextChangedListener(this);
c = (CheckBox) findViewById(R.id.checkBox);
c.setOnClickListener(this);
s1 = (Spinner) findViewById(R.id.spinner);
ArrayAdapter adap = ArrayAdapter.createFromResource(this, R.array.days1, android.R.layout.simple_spinner_item);
s1.setAdapter(adap);
s1.setOnItemSelectedListener(this);
String days[] = {"sunday", "monday", "tuesday"};
s2 = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<String> ada = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, days);
s2.setAdapter(ada);
s2.setOnItemSelectedListener(this);
t = (ToggleButton) findViewById(R.id.tb);
l = (LinearLayout) findViewById(R.id.layout);
t.setOnCheckedChangeListener(this);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
Toast.makeText(getApplicationContext(), "before", Toast.LENGTH_SHORT).show();
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Toast.makeText(getApplicationContext(), "change", Toast.LENGTH_SHORT).show();
}
@Override
public void afterTextChanged(Editable s) {
//Toast.makeText(getApplicationContext(),"after",Toast.LENGTH_SHORT);
try {
int no = Integer.parseInt(s.toString());
if (no > 100) {
s.replace(0, s.length(), "100");
}
} catch (NumberFormatException e) {
}
}
@Override
public void onClick(View v) {
CheckBox t = (CheckBox) v;
if (t.isChecked()) {
Toast.makeText(getApplicationContext(), "Coffee with sugar", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Coffee without sugar", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (parent.getId() == R.id.spinner) {
//Log.d("hi", "item selected");
TextView txt = (TextView) view;
Log.d("spin1", txt.getText().toString());
Toast.makeText(getApplicationContext(), "u have selected" + txt.getText(), Toast.LENGTH_SHORT).show();
} else if (parent.getId() == R.id.spinner1) {
CheckedTextView txt1 = (CheckedTextView) view;
Toast.makeText(getApplicationContext(), "u have selected" + txt1.getText(), Toast.LENGTH_SHORT).show();
Log.d("spin2", txt1.getText().toString());
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
l.setBackgroundColor(Color.BLACK);
} else {
l.setBackgroundColor(Color.WHITE);
}
}
}
这里是xml布局
<?xml version="1.0" encoding="utf-8"?>
<android.widget.LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.dhanya.edittextproject.MainActivity">
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/edittext_bg"
android:ems="10"
android:hint="enter percentage [75%]"
android:inputType="number"
android:maxLength="100"
android:maxLines="2" />
<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sugar" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I want coffee with" />
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Spinner
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"></Spinner>
<ToggleButton
android:id="@+id/tb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textOff="change to dark color"
android:textOn="change to light color" />
</android.widget.LinearLayout>
这里是strings.xml
<resources>
<string name="app_name">EdittextProject</string>
<string-array name="days1">
<item>Blue</item>
<item>Green</item>
<item>Red</item>
</string-array>
</resources>
【讨论】: